Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Get the current URL or route in TWIG template?

Tags:

php

twig

symfony

My route is

admin:
      path:     /admin/
      defaults: { _controller: CatalogWebBundle:Admin:admin }

How I can get route name in PHP template ?

like image 575
Wizard Avatar asked Mar 07 '15 09:03

Wizard


2 Answers

To get the Route Name in Symfony2 enter the following code snippet

$request = $this->container->get('request');
$routeName = $request->get('_route');

To get the URL in Symfony2,

$request = $this->container->get('request');
$routeURL = $request->getRequestUri();
like image 80
Nasik Shafeek Avatar answered Oct 08 '22 17:10

Nasik Shafeek


On symfony5 you can do this.

Calling a controller block and passing current url:

{{ render_esi(controller('App\\Controller\\Frontend\\BlockController::social',{'pageUri': app.request.uri })) }}

and

<?php

namespace App\Controller\Frontend;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class BlockController extends AbstractController {
    public function social($pageUri) {
        return $this->render('block/_social.html.twig', ['pageUri' => $pageUri]);
    }

}

Output twig: 'block/_social.html.twig'

<small>Current Url : {{ pageUri  }}</small>
like image 37
Sudhakar Krishnan Avatar answered Oct 08 '22 19:10

Sudhakar Krishnan