此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:
这是函数骨架:
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屋!