0

I'm having trouble adding an optional parameter to my home route.

This is my current router:

'routes' => [
    'home' => [
        'type' => Segment::class,
        'options' => [
            'route' => '/[:salon/]',
            'constraints' => [
                'salon' => '[a-zA-Z][a-zA-Z0-9_-]*'
            ],
            'defaults' => [
                'controller' => 'Application\Controller\Index',
                'action'     => 'index',
                'salon'      => 'test'
            ],
        ],
    ],
    'application' => [
        'type'    => Segment::class,
        'options' => [
            'route' => '/application[/:controller[/:action]]',
            'constraints' => [
                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
            ],
            'defaults' => [
                '__NAMESPACE__' => 'Application\Controller',
                'controller' => 'Application\Controller\Index',
                'action'     => 'index',
            ],
        ],
        'may_terminate' => true,
        'child_routes' => [
            'default' => [
                'type' => 'wildcard'
            ]
        ]
    ],
],

My Controller:

<?php

namespace Application\Controller;

class IndexController extends AbstractController
{
    public function indexAction()
    {
        var_dump($this->params('salon'));
        die;
    }
}

domain.ltd/ This works and I'm getting default value for salon paramter which is 'test'

domain.ltd/test123 Expected value would be 'test123' but this displays me 404 error: The requested URL could not be matched by routing.

MulOnPomm
  • 145
  • 3
  • 16
  • Could you tell us how is your controller structured? – Alain Pomirol Jan 31 '19 at 22:57
  • If you go to `example.com/test123`, as per question, then you need: `Application\Controller\IndexController#test123Action` function. Not sure why, but you're allowing users to define the names of actions with your action constraints when they're not "index" (here: `'action' => '[a-zA-Z][a-zA-Z0-9_-]*'`). I suggest you rewrite this to be explicit, e.g. have an action `index` in an `IndexController` and an action `add` in an `AddController`, etc. None of this fuzzy user-logic. – rkeet Feb 02 '19 at 22:26
  • I dont want to create test123Action. My goal is to get value 'test123' in to Application\Controller\IndexController#indexAction – MulOnPomm Feb 08 '19 at 08:18

0 Answers0