Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

keevitaja's avatar

Get URI from route name

Hi,

while there is a UrlGenerator which converts route name to URL, is there a way to easily get URI?

0 likes
14 replies
JeffreyWay's avatar

Lots of options, like Request::url(), Request::fullUrl(), Request::path().

5 likes
xingfucoder's avatar

Hi @keevitaja,

Using the method inyection with the new Router class I would make as follow.

In the BaseController you can add the following method:

    public function routeToURI(Router $router) 
   {
            return str_replace('.', '/', $router->currentRouteName());
    }

Don't forget import the Illuminate\Routing\Router class.

You could make the full path using the request object.

keevitaja's avatar

@codeatbusiness route name and uri do not have to match! So this solution does not do it!

@TravisBlasingame this gives me weird result: reminders/{reminders}/token/{token}/create

Without stuff between {} it would be ok.

edit: Currently i get the URL and remove the protocol and domain. So i can get the URI... Just wondering if there is a better solution.

TravisBlasingame's avatar
Level 3

\URL::route($route, [], false);

The false is for the $absolute flag in the UrlGenerator. Without absolute, it just returns the Uri.

7 likes
bfalcao's avatar

$router = app()->make('router');

dd($router->getCurrentRoute()->uri);

... from 2 years ago, but in Laravel 5.4 that's a way to obtain the uri :)

vladshoob's avatar

Neigher of methods worked for me in Laravel 5.5. I was able to find URI by this:


uri_for(route('ua.index'))->getPath();

Please or to participate in this conversation.