What's wrong with this Symfony2 route?

萝らか妹 提交于 2019-12-08 06:01:28

问题


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

Here is the function skeleton:

public function newsArticleAction($id, $slug)
{

}

What am I missing here? I get a 500 error and the log says:

[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 [] []


回答1:


This error comes up not when matching a URL to a route, but when generating a URL from a route.

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} }}

while it has to look like:

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


来源:https://stackoverflow.com/questions/13150543/whats-wrong-with-this-symfony2-route

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!