简体   繁体   中英

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. 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/" . Why /hello/ not loads in this case and how can I fix this?

That's correct You can load /hello though. It is not an error to fix.

Using hard coded URL is not a good practice. You should use generated URLs.

To generate URL from controller USE:

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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