这条 Symfony2 路线有什么问题? [英] What's wrong with this Symfony2 route?
问题描述
我已经使用注释设置了一条路线.在我看来是正确的,Symfony2 说这是错误的.路线如下:
I've set up a route using annotations. Looks right to me, Symfony2 says it's wrong. Here's the route:
@Route("/news/{id}/{slug}", name="newsarticle")
这是我认为与路线匹配的示例网址:
Here's a sample URL which I think matches the route:
http://somesite.com/news/202/my-news-title
这里是函数骨架:
public function newsArticleAction($id, $slug)
{
}
我在这里错过了什么?我收到 500 错误,日志显示:
What am I missing here? I get a 500 error and the log says:
[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:来自侦听器:newsarticle"路由缺少一些必需参数(id").[] [][2012-10-30 20:36:36] request.CRITICAL:Symfony\Component\Routing\Exception\MissingMandatoryParametersException:newsarticle"路由缺少一些必需参数(id").(未捕获的异常)在/home/user/app/cache/prod/classes.php 第 676 行 [] []
[2012-10-30 20:36:35] request.INFO: Matched route "newsarticle" (parameters: "_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 mandatory parameters ("id"). [] [] [2012-10-30 20:36:36] request.CRITICAL: Symfony\Component\Routing\Exception\MissingMandatoryParametersException: The "newsarticle" route has some missing mandatory parameters ("id"). (uncaught exception) at /home/user/app/cache/prod/classes.php line 676 [] []
推荐答案
这个错误不是在匹配 URL 到路由时出现,而是在从路由生成 URL 时出现.
This error comes up not when matching a URL to a route, but when generating a URL from a route.
在您的项目中搜索 path('newsarticle'
或 generateUrl('newsarticle'
).您应该会尝试在不传递所有需要的参数的情况下生成 URL — 某事喜欢:
Search your project for path('newsarticle'
or generateUrl('newsarticle'
. You should find an attempt to generate a URL without passing all the needed parameters — something like:
{{ path('newsarticle', {'slug': news.slug} }}
虽然它必须看起来像:
{{ path('newsarticle', {'id': news.id, 'slug': news.slug} }}
这篇关于这条 Symfony2 路线有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!