Search code examples
phphttpzend-frameworkrouteszend-framework3

zend framework 3 segment routing doesn't work


I have the following module.config.php :

return [
    'router' => [
        'routes' => [
            'landingpage' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/landingpage[/:action/:id]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[a-zA-Z0-9_-]*'
                    ],
                    'defaults' => [
                        'controller' => Controller\LandingPageController::class,
                        'action' => 'index'
                    ]
                ],
                'may_terminate' => true,
            ]
        ]
    ],
    'controllers' => [
        'factories' => [
            Controller\LandingPageController::class => LandingPageControllerFactory::class
        ]
    ],
    'service_manager' => [
        'invokables' => [
            'LandingPage\Service\LandingPageService' => 'LandingPage\Service\LandingPageService'
        ]
    ]
];

I am trying to use the following route and it doesn't work:

http://localhost:8081/landingpage/show/1CGe2cveQ

If I use the following route it works :

http://localhost:8081/landingpage/show

If I use the previous route with a / it doesn't work:

http://localhost:8081/landingpage/show/

If you need more info let me know. Thanks.


Solution

  • You have a double slash in the route declaration: the route is matched by /landingpage/ followed by /:action/:id. If you remove this double slash, the route will work as expected.

    'route' => '/landingpage[/:action/:id]',
    

    Moreover, I'd suggest you to modify the route declaration to make the id optional:

    'route' => '/landingpage[/:action[/:id]]',
    'constraints' => [
        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
        'id' => '[a-zA-Z0-9_-]+'
    ]
    

    Tested: config

    'landingpage' => [
        'type' => Segment::class,
        'options' => [
            'route' => '/landingpage[/:action[/:id]]',
            'constraints' => [
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                'id' => '[a-zA-Z0-9_-]*'
            ],
            'defaults' => [
                'controller' => Controller\IndexController::class,
                'action' => 'index'
            ]
        ],
        'may_terminate' => true,
    ],
    

    IndexController:

    public function indexAction () {
        print '<pre>' . print_r($this->params()->fromRoute(), true);
        die(); 
    }
    public function showAction(){
        print '<pre>' . print_r($this->params()->fromRoute(), true);
        die();
    }
    

    Calling /landingpage

    Array
    (
        [controller] => Application\Controller\IndexController
        [action] => index
    )
    

    Calling /landingpage/show

    Array
    (
        [controller] => Application\Controller\IndexController
        [action] => show
    )
    

    Calling /landingpage/show/1CGe2cveQ

    Array
    (
        [controller] => Application\Controller\IndexController
        [action] => show
        [id] => 1CGe2cveQ
    )
    

    Don't forget to clear the configuration cache, if it enabled ;)