knete
knete

Reputation: 123

Symfony2: Get Route Path

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?

Upvotes: 5

Views: 7640

Answers (2)

Kep
Kep

Reputation: 5857

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

Upvotes: 8

kamwoz
kamwoz

Reputation: 226

$request->getPathInfo();

You want to get it from router or request ?

Upvotes: 1

Related Questions