Php zf2段路线捕捉id位于最后位置 我希望我的URL路由有一个动态的部分,并在同一页面上结束,不管我在URL的中间部分有什么。

Php zf2段路线捕捉id位于最后位置 我希望我的URL路由有一个动态的部分,并在同一页面上结束,不管我在URL的中间部分有什么。,php,zend-framework2,zend-route,Php,Zend Framework2,Zend Route,例如: /en/旧类别/旧名称/pid/123123 /en/新类别/now-in-a-sub-category/新名称/pid/123123 如果两者都被同一控制器/操作捕获(如有必要,将发布相关的301/302) 我当前的路由器包含: 'router' => [ 'routes' => [ 'blog' => [ 'type' => 'segment', 'options' =&

例如:

/en/旧类别/旧名称/pid/123123

/en/新类别/now-in-a-sub-category/新名称/pid/123123

如果两者都被同一控制器/操作捕获(如有必要,将发布相关的301/302)

我当前的路由器包含:

'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'
                        ]
                    ]
                ]
            ]
        ]
    ]
]
但它不起作用。
/
/en
被正确捕获,但我前面提出的子例程却没有被正确捕获


我在做我想做的事情的正确道路上吗?我应该改为编写正则表达式路由吗?

+
将不匹配
/
,因为分段路由在应用约束之前会在
/
上拆分路径。要使您的路线正常运行,您需要类似于
/:path/:foo/pid/:postid
的内容。正则表达式路由也可以工作。

+
将不匹配
/
,因为分段路由在应用约束之前在
/
上分割路径。要使您的路线正常运行,您需要类似于
/:path/:foo/pid/:postid
的内容。regex路由也可以工作。

由于在URL的第一部分和最后一部分之间,我需要具有可变数量的段,因此我无法使用
路由执行此操作,但我可以使用
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'
                        ]
                    ]
                ] 
            ] 
        ]
    ]
]
“路由器”=>[
“路线”=>[
“博客”=>[
'type'=>'regex',
“选项”=>[
'正则表达式'=>“/(?[a-z]{2})?”,
'spec'=>“/%language%”,
“默认设置”=>[
'controller'=>'Blog\controller\List',
'操作'=>'索引'
],
“may_terminate”=>true,
],
“子路由”=>[
“详细信息”=>[
'type'=>'regex',
“选项”=>[
“regex'=>'/(?。+)/pid/(?\d+)$”,
'spec'=>'/%path%/pid/%postid%',
“默认设置”=>[
'controller'=>'Blog\controller\List',
“操作”=>“详细信息”
]
]
] 
] 
]
]
]

由于在URL的第一部分和最后一部分之间,我需要具有可变数量的段,因此我无法使用
路由来实现这一点,但我可以使用
正则表达式
路由来管理它,配置如下:

'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'
                        ]
                    ]
                ] 
            ] 
        ]
    ]
]
“路由器”=>[
“路线”=>[
“博客”=>[
'type'=>'regex',
“选项”=>[
'正则表达式'=>“/(?[a-z]{2})?”,
'spec'=>“/%language%”,
“默认设置”=>[
'controller'=>'Blog\controller\List',
'操作'=>'索引'
],
“may_terminate”=>true,
],
“子路由”=>[
“详细信息”=>[
'type'=>'regex',
“选项”=>[
“regex'=>'/(?。+)/pid/(?\d+)$”,
'spec'=>'/%path%/pid/%postid%',
“默认设置”=>[
'controller'=>'Blog\controller\List',
“操作”=>“详细信息”
]
]
] 
] 
]
]
]

请不要将代码添加到我的答案中,就像它是我发布的一样-我已回滚编辑。该代码应该作为一个单独的答案发布,然后您可以接受。我的答案解释了为什么您试图做的事情不起作用,并提供了一个可能的解决方案(由于您当时提供的其他信息,结果没有帮到您)。很抱歉,你觉得这“几乎一文不值”,但有了你的新答案,我们现在有了一个已回答的问题,这将有助于解决类似问题的其他人。在我看来,编辑他人问题/答案的能力主要是为了提高可读性(例如,打字错误、格式)。请不要在我的答案中添加代码,就像它是我发布的一样-我已回滚编辑。该代码应该作为一个单独的答案发布,然后您可以接受。我的答案解释了为什么您试图做的事情不起作用,并提供了一个可能的解决方案(由于您当时提供的其他信息,结果没有帮到您)。很抱歉,你觉得这“几乎一文不值”,但有了你的新答案,我们现在有了一个已回答的问题,这将有助于解决类似问题的其他人。在我看来,编辑他人问题/答案的能力主要是为了提高可读性(例如,打字错误、格式)。我很高兴你能做到这一点。仅供参考,语言路线可能仍然是段路线-子路线不需要是同一类型的。我很高兴你能让它工作。仅供参考,语言路线可能仍然是段路线-子路线不需要相同类型。