我试图在路由占位符中使用连字符名称,如new-jersey
,但遇到404错误。当我在占位符中使用单个世界(如illinois
)时,该路线可以正常工作。
这是因为PHP试图处理带连字符的占位符变量(如new-jersey
)时出错了吗?有没有一种方法可以将连字符的变量传递到路由占位符中?
我的相关路线设置如下:
# routing.yml
bar_exam_show:
path: /pass-the-bar-exam/{statePath}-bar-exam
defaults: { _controller: AppBundle:BarExams:show }
综上所述:
/illinois-bar-exam #200 OK
/new-jersey-bar-exam #404 Not Found
/district-of-columbia #404 Not Found
编辑-UrlMatcher.php的内容
if (0 === strpos($pathinfo, '/pass-the-bar-exam')) {
// bar_exams_index
if ($pathinfo === '/pass-the-bar-exam') {
return array ( '_controller' => 'AppBundle\Controller\BarExamsController::indexAction', '_route' => 'bar_exams_index',);
}
// bar_exams_show
if (preg_match('#^/pass\-the\-bar\-exam/(?P<statePath>[^/\-]++)\-bar\-exam$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar_exams_show')), array ( '_controller' => 'AppBundle\Controller\BarExamsController::showAction',));
}
}
奇怪的是,symfony在缓存中添加了对连字符的排除。你一定在使用一个旧得多的版本,因为它不再这样做了。
无论如何,添加此要求应该覆盖默认表达式。
bar_exam_show:
path: /pass-the-bar-exam/{statePath}-bar-exam
defaults: { _controller: AppBundle:BarExams:show }
requirements:
page: [^/]+