1

我已经使用注释设置了一条路线。在我看来是对的,Symfony2 说这是错误的。这是路线:

@Route("/news/{id}/{slug}", name="newsarticle")

这是我认为与路线匹配的示例 URL:

http://somesite.com/news/202/my-news-title

这是功能骨架:

public function newsArticleAction($id, $slug)
{

}

我在这里想念什么?我收到 500 错误,日志显示:

[2012-10-30 20:36:35] request.INFO:匹配路由“newsarticle”(参数:“_controller”:“App\SiteBundle\Controller\DefaultController::newsArticleAction”,“id”:“202”,“ slug": "my-news-title", "_route": "newsarticle") [] [] [2012-10-30 20:36:36] app.INFO: From listener: The "newsarticle" route has some missing强制参数(“id”)。[] [] [2012-10-30 20:36:36] request.CRITICAL:Symfony\Component\Routing\Exception\MissingMandatoryParametersException:“newsarticle”路由缺少一些强制参数(“id”)。(未捕获的异常)在 /home/user/app/cache/prod/classes.php 第 676 行 [] []

4

1 回答 1

10

此错误不是在将 URL 与路由匹配时出现,而是在从路由生成 URL 时出现。

在您的项目中搜索path('newsarticle'generateUrl('newsarticle'。您应该尝试在不传递所有需要的参数的情况下生成 URL — 类似于:

{{ path('newsarticle', {'slug': news.slug} }}

虽然它必须看起来像:

{{ path('newsarticle', {'id': news.id, 'slug': news.slug} }}
于 2012-10-31T09:48:51.303 回答