简体   繁体   English

zf2段路线在最后位置上的捕获ID

[英]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. 我希望自己的url路由具有动态部分,并最终出现在同一页面上,无论URL的中间部分是否包含内容。

Eg: 例如:

/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). 应该由相同的控制器/动作捕获(如果需要,它将发出相关的301/302)。

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. //en被正确捕获,但是像我之前提出的子路由却没有被捕获。

Am I in the right path to do what I want to do? 我在正确的道路上做自己想做的事情吗? Should I write a regex route instead? 我应该改写正则表达式路由吗?

.+ won't match / because segment routes split the path on / before applying constraints. .+不会匹配/因为细分路线会在应用约束之前在/上分割路径。 To make your route work you'll need something like /:path/:foo/pid/:postid . 要使路由正常工作,您需要/:path/:foo/pid/:postid A regex route might also work. 正则表达式路由也可能有效。

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: 由于在URL的第一部分和最后一部分之间,我需要具有可变数量的段,因此无法使用segment路由来做到这一点,但是我可以使用regex路由来对其进行管理,配置如下:

'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'
                        ]
                    ]
                ] 
            ] 
        ]
    ]
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM