How to Get Current Route in Symfony 2

How to get current route in Symfony 2?

From something that is ContainerAware (like a controller):

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

Symfony 2 - How can I get current route name using bundle JMSI18nRoutingBundle

It's ok, I found the solution :

$actual_link = preg_replace('/\/app_dev.php/', '', $_SERVER['REQUEST_URI']);
$route = $this->get('router')->match($actual_link)['_route'];

First line : "app_dev.php" has to be removed if we are in dev mode.

Second line : Match current URL with routing to find the current route name.

Get the route name in a Symfony service

If you use symfony4, and autowire (https://symfony.com/doc/current/service_container/autowiring.html), you can inject request_stack service as following:

use Symfony\Component\HttpFoundation\RequestStack;

class Myservice {

private $u;

public function __construct(Utilities $u, RequestStack $requestStack) {

$this->u = $u;

$route = $requestStack->getCurrentRequest()->get('_route');
}
}

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

To get the current URL

$request->getRequestUri(); or app.request.uri

As for the route itself, the best practice is to inject it as parameter in your controller, see the doc here. You could use $request->attributes->get('_route') or app.request.attributes.get('_route') but it is not as reliable, for example it won't work with forwards as you are forwarding to a controller, not to a path. And it is really only meant for debugging purposes according to Fabien (@fabpot), the creator, so I would not rely on it for future upgrades' sake.

Sidenote

Remember to avoid $request->get() anytime you can, so no $request->get('_route') as I've seen in some answers on similar questions

If you don't need the flexibility in controllers, it is better to
explicitly get request parameters from the appropriate public property
instead (attributes, query, request)

The reason being that it will look in said public properties (attributes, query & request) instead of just the one (attributes), making it much slower

Symfony 5 Route name in Controller

It's not recommended to create request inside your controller.
Preferred way of obtaining already created Request is DI and autowiring:

// src/Controller/BlogController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
/**
* @Route("/blog", name="blog_list")
*/
public function list(Request $request)
{
$routeName = $request->attributes->get('_route');
$routeParameters = $request->attributes->get('_route_params');

var_dump($routeName);
}
}

This request is processed by Symfony HttpKernel and filled with additional information.

More info: https://symfony.com/doc/current/routing.html#getting-the-route-name-and-parameters

How did it get there: https://github.com/symfony/symfony/blob/b2609c4bae69ca383b97cb520da2ed9be1c48449/src/Symfony/Component/Routing/Matcher/UrlMatcher.php#L217

How to get root route in symfony 2

turns out How to get current route in Symfony 2? had the answer, just not the accepted one

https://stackoverflow.com/a/11660777/711072 pointed me in the right direction

the trick is passing it through template to controller like

{% render url('_internal_main_navigation', { 'route': app.request.attributes.get('_route') }) %}

and then it can be accessed in controller like

$this->getRequest()->query->get('route');

in controller

Symfony2 + Twig get real/full current route

The Request class has a getRequestUri() method. You can access it in twig like

{{ app.request.requesturi }}


Related Topics



Leave a reply



Submit