php “无法为命名路由生成 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25728172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:58:20  来源:igfitidea点击:

"Unable to generate a URL for the named route

phpsymfony

提问by JParkinson1991

Ive just started playing with symfony2 and im working on adding url etc. I cant seem to get my twig template to pick up on my function when passed its name using @Route. Any ideas why?

我刚开始玩 symfony2,我正在努力添加 url 等。当使用 @Route 传递它的名称时,我似乎无法让我的树枝模板接收我的函数。任何想法为什么?

Controller:

控制器:

/**
* @Route("/cube/{number}", name="get_cubed")
*/
public function indexAction($number)
{
    $cube = $number * $number * $number;
    return $this->render('NumberCubedBundle:Default:index.html.twig',
        array('number' => $number, 'cube' => $cube)
    );
}

My Twig File:

我的树枝文件:

{% extends '::base.html.twig' %}
{% block title %}Cube Number Generator{% endblock %}
{% block body %}
    {{ number }}^3 = {{ cube }}
    <a href="{{ path('get_cubed', { 'number': 40 }) }}">Cube 40</a>
{% endblock %}

The Error:

错误:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "get_cubed" as such route does not exist.") in NumberCubedBundle:Default:index.html.twig at line 5. 

Any help would be massively appreciated. Thanks

任何帮助将不胜感激。谢谢

回答by pid

EDIT:why the annotation didn't work

编辑:为什么注释不起作用

Most probably you didn't include Sensio's vendor bundle as explained in annotation routingand sensio framwork EXTRAbundle.

很可能您没有像注释路由sensio 框架EXTRAbundle 中解释的那样包含 Sensio 的供应商

The default routing mechanism uses the routing file (routing.yml) which does not need the extrabundle.

默认路由机制使用不需要额外包的路由文件(routing.yml)。

The routing through annotations, on the other hand, are considered an additional feature that is not always desired and thus has been extracted to a separate and optional bundle.

另一方面,通过注释的路由被认为是一个并不总是需要的附加功能,因此已被提取到一个单独的和可选的包中。



You need to configure the route in the routing file:

需要在路由文件中配置路由:

app/config/routing.yml

Define the route get_cubedusing the standard Symfony 2 routing syntax.

get_cubed使用标准的 Symfony 2 路由语法定义路由

Much like this:

很像这样:

get_cubed:
    path:      /cube/{number}
    defaults:  { _controller: NumberCubedBundle:Default:index }
    requirements:
        number: \d+

Now you should be able to get the page with the route:

现在您应该能够获得带有路线的页面:

.../app_dev.php/cube/40

回答by gouat

The Solution for pattern annotation is: on app/config/routing.yml add :

模式注释的解决方案是:在 app/config/routing.yml 添加:

tuto_produit_bundle:
    resource: "@ProduitBundle/Controller/"
    type: annotation
    prefix: /cat

and o URL:

和 o 网址:

http://localhost/produits/Symfony/web/app_dev.php/cat/{adresse}/{route action}
like http://localhost/vente%20produits/Symfony/web/app_dev.php/cat/categorie/