5

With the command $this->generateUrl($route); inside a Controller I can get the URL of the Route.

However, I only want to retrieve the last part of it (as specified in routing.yml).

For example, instead of /web/app_dev.php/$path I just want to return /$path

How can I achieve this?

2 Answers 2

8

You could try this:

$route = $this->get('router')->getRouteCollection()->get('routeName');
if ($route)
    echo $route->getPath();

You also could take a look at Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand (class responsible for router:debug console command).

Otherwise: str_replace (or substr) on the first part would also be an option

Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I've been searching for...Thank you!
This works, but it's important to mention that you should not use getRouteCollection() as it worsens the performance: github.com/symfony/symfony-docs/issues/6710
1
$request->getPathInfo();

You want to get it from router or request ?

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.