Variable prefixed to symfony2

I need a variable in the prefix in my symfony2 routing so that I can do something like this in the main routing file:

//app/config/routing.yml god: resource: "@Acme/DemoBundle/Resources/config/routing.yml" prefix: /god/{religion} 

and then something like this in the bundle routing file:

 gods_route_to_heaven: path: /path_to_heaven defaults: { _controller: AcmeBlogBundle:God:show } 

so that I can access the following paths:

 /god/Christianity/path_to_heaven /god/Islam/path_to_heaven /god/Hinduism/path_to_heaven 

etc.

If I type app/console route:debug | grep api app/console route:debug | grep api on the console, I get the correct route /god/{religion}/path_to_heaven , so the route is generated correctly.

But when I try to get the parameter in the controller, placing it as an input to the action function, for example:

 public function showAction($religion) 

the path is damaged and resets the paths, I see that it is duplicated: /god/{religion}/path_to_heaven/{religion}

So how can I get the $ religion variable from inside the controller?

+6
source share
4 answers

Have you tried this?

 //app/config/routing.yml god: resource: "@Acme/DemoBundle/Resources/config/routing.yml" prefix: /god 

and then something like this in the bundle routing file:

 gods_route_to_heaven: path: /{religion}/path_to_heaven defaults: { _controller: AcmeBlogBundle:God:show } 

Let me know if it works.

+3
source

the possible solution you need would be:

File: src / Acme / CoreBundle / Twig / PathExtension.php

 <?php namespace Acme\CoreBundle\Twig\Extension; use Symfony\Bundle\FrameworkBundle\Routing\Router; use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpKernel\HttpKernel; class PathExtension extends \Twig_Extension { private $request; private $router; public function __construct(Router $router) { $this->router = $router; } public function onKernelRequest(GetResponseEvent $event) { if ($event->getRequestType() === HttpKernel::MASTER_REQUEST) { $this->request = $event->getRequest(); } } public function getFunctions() { return array( 'path_route' => new \Twig_Function_Method($this, 'getPath') ); } public function getPath($name, $parameters = array()) { $parameters = array_merge($parameters, [ 'religion' => $this->request->get('religion'), ]); return $this->router->generate($name, $parameters, false); } public function getName() { return 'twig_path_route_extension'; } } 

Configuration as a Service:

File: src / Acme / CoreBundle / Resources / config / services.yml

 services: twig.path_route_extension: class: Acme\CoreBundle\Twig\PathExtension tags: - { name: twig.extension } - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } arguments: [@router] 

Set routing parameters:

File: app / config / routing.yml

 _acme_religion: resource: "@AcmeCoreBundle/Resources/config/routing.yml" prefix: /god/{religion}/ 

Then you can use it in templates and routing.yml, for example:

 _religion_pathheaven: path: /path_to_heaven defaults: { _controller: AcmeCoreBundle:System:getpathheaven } 

Then in your templates you can use:

 <a href="{{ path_route('_religion_pathheaven') }}"> Link Path to heaven. </a> 

Literature:

Automatically pass path parameters http://blog.viison.com/post/15619033835/symfony2-twig-extension-switch-locale-current-route

+2
source

You can solve this problem this way -

 god: resource: "@Acme/DemoBundle/Resources/config/routing.yml" prefix: /god/{_locale}/ 

no need to change here -

 gods_route_to_heaven: path: /path_to_heaven defaults: { _controller: AcmeBlogBundle:God:show } 

here -

  public function showAction(){ // below line is extra if you need $post = $this->get('request')->request->all(); $religion = $post['religion']; } 

I have not tested this code, but the whole process is well proven. If you see any problem, discuss it.

You can also see this question, which will help you a lot. Requirements for Symfony2 Route global {_locale}

+1
source

It may be late, but it can help other people.
I ran into the same issue with Symfony and FOSUserBundle.
In the FOSUserBundle profile link:

site_url / profile /
site_url / profile / edit
site_url / profile / switch password

I have an admin panel and toolbars, and I want the links to be in the admin panel

site_url / admin / profile /
site_url / admin / profile / edit
site_url / admin / profile / switch password

and in the user panel

site_url / dashboard / profile /
site_url / dashboard / profile / edit
site_url / dashboard / profile / switch password

To get this behavior, it is very simple without EventListener and TwigExtesion
Here's the solution:

replace

 fos_user: resource: "@FOSUserBundle/Resources/config/routing/all.xml" 

by

 fos_user_security: resource: "@FOSUserBundle/Resources/config/routing/security.xml" fos_user_registration: resource: "@FOSUserBundle/Resources/config/routing/registration.xml" fos_user_resetting: resource: "@FOSUserBundle/Resources/config/routing/resetting.xml" fos_user_change_password: resource: "@FOSUserBundle/Resources/config/routing/change_password.xml" prefix: /{prefix}/profile fos_user_profile: resource: "@FOSUserBundle/Resources/config/routing/profile.xml" prefix: /{prefix}/profile 

In the template use:

 <a href="{{ path('fos_user_profile_show', {'prefix': 'dashboard'}) }}">Show Profile</a> <a href="{{ path('fos_user_profile_edit', {'prefix': 'dashboard'}) }}">Edit Profile</a> <a href="{{ path('fos_user_change_password', {'prefix': 'dashboard'}) }}">Change Password</a> 

Or any other prefix you want
Now, if you use, for example, a link to change the password, you will get this error

During template rendering, an exception was thrown ("Some required parameters are missing (" prefix ") for creating a URL for the route" fos_user_change_password ".") In FOSUserBundle: ChangePassword: changePassword_content.html.twig on line 3.

you can add a prefix to the template as follows:
replace

 <form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password"> 

by

 <form action="{{ path('fos_user_change_password', {'prefix': app.request.attributes.get('prefix') }) }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password"> 

it will give you this error

Some required parameters are missing ("prefix") for creating the URL for route "fos_user_profile_show".

the final solution I found is to override the controller
just copy the hole in the application controller folder, change the name and replace

 if (null === $response = $event->getResponse()) { $url = $this->generateUrl('fos_user_profile_show'); $response = new RedirectResponse($url); } 

by

 if (null === $response = $event->getResponse()) { $url = $this->generateUrl('fos_user_profile_show',array('prefix'=>$request->get('prefix'))); $response = new RedirectResponse($url); } 

in a form in which you don’t need an action, so it will be

 <form {{ form_enctype(form) }} method="POST" class="fos_user_change_password"> 
0
source

All Articles