My route is
admin:
path: /admin/
defaults: { _controller: CatalogWebBundle:Admin:admin }
How I can get route name in PHP template ?
My route is
admin:
path: /admin/
defaults: { _controller: CatalogWebBundle:Admin:admin }
How I can get route name in PHP 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
Not a good thing to do directly in Twig but you can still do. The better way is to pass it as an argument from the controller.
Get route parameters in Twig.
{{ app.request.attributes.get('_route_params') }}
AND
Gets whole bundle name in Twig.
{{ app.request.attributes.get("_controller") }}
Get route name in Twig.
{{ app.request.attributes.get('_route') }}
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();
Adding that in some cases, app.request.uri
won't return the url of the current page.
Example: in your page template, you call a controller via:
{{ render(controller('AppBundle:MyController:myBlock')) }}
And in myBlockAction
, you render another template, say block.html.twig
.
A call to app.request.uri
from block.html.twig
will display something like:
http://www.example.com/app.php/_fragment?_path=_format%3Dhtml%26_locale%3Dfr%26_controller%3DAppBundle%253AMyController%253AmyBlock
If you want to get the absolute url of the rendered page from inside block.html.twig
, you can reassemble it from php $_SERVER variables:
{{ app.request.server.get('REQUEST_SCHEME') ~ '://' ~
app.request.server.get('SERVER_NAME') ~
app.request.server.get('PHP_SELF') }}
You can also add QUERY_STRING
if needed.
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>