简体   繁体   English

使用前缀时,Symfony Route参数不起作用

[英]Symfony Route parameters not working when prefix is used

When I use this configuration: 当我使用此配置时:

#/app/config/routing.yml
_org_demo:
    resource: "@OrgDemoBundle/config/routing.yml"

#Org/DemoBundle/config/routing.yml
_home:
    path: /{name}
    defaults: { _controller: OrgDemoBundle:Home:index, name: world}

Both / and /xyz loads fine. //xyz加载都很好。 But, when I use the new configuration 但是,当我使用新配置时

#/app/config/routing.yml
_org_demo:
    resource: "@OrgDemoBundle/config/routing.yml"
    prefix: /hello

#Org/DemoBundle/config/routing.yml
_home:
    path: /{name}
    defaults: { _controller: OrgDemoBundle:Home:index, name: world}

In this case /hello/xyz load but not /hello/ and I get error No route found for "GET /hello/" . 在这种情况下/hello/xyz加载但不是/hello/并且我得到错误No route found for "GET /hello/" Why /hello/ not loads in this case and how can I fix this? 在这种情况下为什么/hello/不加载以及如何解决这个问题?

That's correct You can load /hello though. 这是正确的你可以加载/hello It is not an error to fix. 修复不是错误。

Using hard coded URL is not a good practice. 使用硬编码的URL不是一个好习惯。 You should use generated URLs. 您应该使用生成的URL。

To generate URL from controller USE: 从控制器USE生成URL:

$this->generateUrl('_home');   //Will return /helo
$this->generateUrl('_home', array('name' => 'Bangladesh')) // will return /hello/Bangladesh

On twig template you can use these to have similar output : 在树枝模板上,您可以使用它们来获得类似的输出:

{{ path('_home') }}
{{ path('_home', {name: 'Bangladesh' }) }}

If you like to handle both urls you can use this cookbook 如果您想要处理这两个网址,可以使用此菜谱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM