有没有办法在 Symfony2 2.3 路由占位符中使用连字符?

Is there a way to use hyphens in a Symfony2 2.3 route placeholder?

我试图在我的路线占位符中使用带连字符的名称,例如 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:  [^\/]+