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
How to Change Maximum Number of Post Variable in PHP
PHP Pdo Prepared Statement - MySQL Like Query
Running Command-Line Application from PHP as Specific User
Fatal Error: Cannot Use Object of Type MySQLi_Result
MySQLi_Fetch_Array While Loop Columns
How to Setup Elasticsearch Index Structure With Multiple Entity Bindings
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter
Laravel Migration: Unique Key Is Too Long, Even If Specified
How to Import a .SQL File in MySQL Database Using PHP
How to Override the Path of PHP to Use the Mamp Path
How to Get Last Key in an Array
PHP: If Internet Explorer 6, 7, 8 , or 9
How to Get Url of Current Page in PHP