Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the Twig path() function from a Controller?

Tags:

Okay, I know I can't literally call a twig template function from a controller, but to make links, I usually do the {{ path('_routeName') }} and that's great.

However, now I want to formulate some links in the controller that will then be passed to the template via parameters like this:

$params = array(
    'breadcrumbs' = array(
        'Donuts' => '/donuts',
        'Bearclaws' => '/donuts/bearclaws',
        'Strawberry bearclaw' => null,
    ),
);
return $this->render('Bundle:Donut:info.html.twig', $params);

Except I don't want to hard-code those links. What I'd like is to be able to do

        'Donuts' => path('_donutRoute'),

but how to reach the path method or equivalent?

like image 813
Robert Martin Avatar asked Sep 07 '11 12:09

Robert Martin


People also ask

How do I get a twig URL?

You can get the current URL in Twig/Silex 2 like this: global. request. attributes. get('_route') .

What is Twig in Symfony?

It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher. Symfony PHP framework comes with a bundled support for Twig as its default template engine since version 2. Twig. Original author(s)

What is a twig extension?

Twig is a template framework and is a direct replacement for PHP template. Twig extension provides more flexibility to process almost anything inside twig. Twig extends in many ways such as tags, filters, operators, global variables, and functions.


1 Answers

If your controller is extending the Symfony2 Controller (Symfony\Bundle\FrameworkBundle\Controller\Controller) you can use the following to generate urls like this :

$this->generateUrl('_donutRoute')

like image 168
d.syph.3r Avatar answered Oct 10 '22 02:10

d.syph.3r