17

How can I get the current URL from a Twig template?

I am using Twig with PHP, without any other framework.

1
  • Why not pass in the current URL as a template variable? Or you could write a template tag that outputs the URL. Commented Feb 18, 2012 at 7:09

5 Answers 5

28

The following works in Silex and most certainly in Symfony2 as they share the Request class (I did not test though) :

{{ app.request.getRequestUri() }}
Sign up to request clarification or add additional context in comments.

2 Comments

{{ app.request.requestUri }}
It's deprecated, one should not use it. github.com/silexphp/Silex/issues/1257#issuecomment-141058724
7

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.

$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.

2 Comments

I wonder if there is any internal tool (variable ot method) to get URL from Twig?
You might find some code already written for the purpose, but retrieving the current URL is not within the scope of a template engine, so I highly doubt Twig has anything built in. I certainly couldn't find anything.
4

Go http://api.symfony.com/2.3/Symfony/Component/HttpFoundation/Request.html

or : {{ app.request.getUri() }} for full Uri.

Comments

2

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.

As of Symfony 2.4, the best practice is to never inject the request service, but to inject the request_stack service instead [...]

So in a Silex application it could be achieved with :

app.request_stack.getCurrentRequest.getUri

Comments

0

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 :

$app = new Silex\Application();
// add the current url to the app object.
$app['current_url'] = $_SERVER['REQUEST_URI'];

Then in your Twig template you can do

{{ app.current_url }}

Let me know what is the botom line of this method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.