9951 explained code solutions for 126 technologies

php-symfonyHow to get the current URL in PHP Symfony?

The current URL in PHP Symfony can be obtained using the getPathInfo() method of the Request object.

Example code

$request = Request::createFromGlobals();
$currentUrl = $request->getPathInfo();

Output example


Code explanation

  • Request::createFromGlobals(): creates a new Request object from the PHP global variables.
  • getPathInfo(): returns the path being requested relative to the executed script.

Helpful links

Edit this code on GitHub