How can I get the current URL from a Twig template?
I am using Twig with PHP, without any other framework.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Finding the current URL
The current URL is supplied by your web server and written to the
$_SERVERsuper-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
$_SERVERvalues 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.