Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable on a prefix on symfony2 routing

I need a variable on a prefix on 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 paths like this:

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

and so on.

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

But when I try to get the parameter in the controller by placing it as an input in the action function like this:

public function showAction($religion)

the path gets corrupted and dumping the paths i see it gets duplicated: /god/{religion}/path_to_heaven/{religion}

So how would i get the $religion variable from inside the controller?

like image 770
Noquepoaqui Avatar asked Jan 27 '14 18:01

Noquepoaqui


2 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.

like image 59
Dani Sancas Avatar answered Oct 13 '22 11:10

Dani Sancas


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 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 your routing options:

File: app/config/routing.yml

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

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

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

References:

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

like image 30
adonisfigueroa Avatar answered Oct 13 '22 11:10

adonisfigueroa