Search code examples
phpzend-framework2zend-route

zf2 segment route catching id on last position


I want my url route to have a dynamic part and to end up on the same page not matter if whatever I have in the middle part of my URL.

E.g.:

/en/the-old-category/the-old-name/pid/123123123

/en/the-new-category/now-in-a-sub-category/the-new-name/pid/123123123

Should both get caught by the same controller/action (which will issue the pertinent 301/302 if necessary).

My current router contains:

'router'       => [
    'routes' => [
        'blog' => [
            'type'    => 'segment',
            'options' => [
                'route'         => "/[:language]",
                'constraints'   => [
                    'language' => '[a-z]{2}'
                ],
                'defaults'      => [
                    'controller' => 'Blog\Controller\List',
                    'action'     => 'index',
                    'language'   => 'en'
                ],
                'may_terminate' => true,
                'child_routes'  => [
                    'detail'  => 'segment',
                    'options' => [
                        'route'       => '/:path/pid/:postid',
                        'constraints' => [
                            'path'   => '.+',
                            'postid' => '\d{1,10}'
                        ],
                        'defaults'    => [
                            'controller' => 'Blog\Controller\List',
                            'action'     => 'detail'
                        ]
                    ]
                ]
            ]
        ]
    ]
]

But it's not working. / and /en are being caught properly, but subroutes like the ones I proposed earlier, are not.

Am I in the right path to do what I want to do? Should I write a regex route instead?


Solution

  • Since between the first and the last part of my URL I need to have a variable number of segments, I can't do this with a segment route, but I was able to manage it with a regex route, configured as follows:

    'router' => [
        'routes' => [
            'blog' => [
                'type'         => 'regex',
                'options'      => [
                    'regex'         => "/(?<language>[a-z]{2})?",
                    'spec'          => "/%language%",
                    'defaults'      => [
                        'controller' => 'Blog\Controller\List',
                        'action'     => 'index'
                    ],
                    'may_terminate' => true,
                ],
                'child_routes' => [
                    'detail' => [
                        'type'    => 'regex',
                        'options' => [
                            'regex'    => '/(?<path>.+)/pid/(?<postid>\d+)$',
                            'spec'     => '/%path%/pid/%postid%',
                            'defaults' => [
                                'controller' => 'Blog\Controller\List',
                                'action'     => 'detail'
                            ]
                        ]
                    ] 
                ] 
            ]
        ]
    ]