The current URL is supplied by your web server and written to the $_SERVER super-global. Run this small script, <?php echo '<pre>'; print_r($_SERVER);, through your server and root around to find the value(s) you are looking for.
After you have the URL, you need to pass it as a template variable when calling render(...) on the Twig template instance. For example, you might code this.
$current_url = // figure out what the current url is
// pass the current URL as a variable to the template
echo $template->render(array('current_url' => $current_url));
To use the variable in the template, you use the {{ variable_name }} syntax.
The following works in Silex and most certainly in Symfony2 as they share the Request class (I did not test though) :
Keeping best practice in mind, at this time you should use
Symfony\Component\HttpFoundation\RequestStack
.See http://symfony.com/blog/new-in-symfony-2-4-the-request-stack.
So in a Silex application it could be achieved with :
Here something I found to make it generic with the sliex Framework. I guess my solution is not perfect but it's get the job done.
in your PHP code add this code :
Then in your Twig template you can do
Let me know what is the botom line of this method.
Go http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html
or :
{{ app.request.getUri() }}
for full Uri.Finding the current URL
The current URL is supplied by your web server and written to the
$_SERVER
super-global. Run this small script,<?php echo '<pre>'; print_r($_SERVER);
, through your server and root around to find the value(s) you are looking for.Related questions on this subject:
The PHP manual describes the nature of the available
$_SERVER
values here.Getting the URL in TWIG
After you have the URL, you need to pass it as a template variable when calling
render(...)
on the Twig template instance. For example, you might code this.To use the variable in the template, you use the
{{ variable_name }}
syntax.