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

ibanks's avatar

Getting Only The Path From Named Route

Hello guys.

I mistakenly thought that I could do this through the route() helper function but that gives me the fully qualified url. I only want to get the path (i.e. http://domain.com/url/path)

Any help is very much appreciated.

0 likes
9 replies
ctroms's avatar

You can get it from the request Check out the docs for more info.

$uri = $request->path();
ibanks's avatar

Thanks for your reply.

Maybe I should've been a little more clear. Sorry. The purpose of me getting the path only of a named route is so I can reference only the name of the route whenever I would like to do redirections throughout the application. For instance

public function fooMethod ()
{
    ...some validation code...

    ...once validation has passed...

    ...redirect here...

    return redirect({namedRoute});


}

The named route of course is described in the route file:

Route::get('/url/path', Controller@method)->name('namedRoute');

Where namedRoute would give me the path assigned to it. I'm wanting to do this just in case I need to change the name of the url paths at any time. I therefore wouldn't have to go change the hardcoded path in multiple files.

ctroms's avatar

Sorry. I think I am still a little confused. If you want to redirect to the named route why not just use the named route in your redirect.

return redirect()->route('the.named.route');
ibanks's avatar

Okay. So maybe I just used the function incorrectly?

I used it like so:

return redirect(route('the.named.route'));

Is this right or will I get a different result?

ctroms's avatar

That will get you the same result as what I previously posted. Try it out both ways to prove it.

ctroms's avatar

Sorry. I am still confused as to why you want /url/path. As per your post:

The purpose of me getting the path only of a named route is so I can reference only the name of the route whenever I would like to do redirections throughout the application.

That is precisely what the route() helper function is for. To generate a url based on a named route. You are still free to change the url in your routes.php. As long as you don't change the name of the route, this will continue to work.

edmandie.samonte's avatar

I also have the same goal as this. My intention is that since I have so many links inside my view, having the host/domain part seems to be so redundant and a waste of bandwidth.

edmandie.samonte's avatar

Ooops! Found it in Laravel 5.3

route($name, $parameters = [], $absolute = true)

set the third parameter to false

<a href="{{ route('namedRoute',[],false) }}" />
25 likes

Please or to participate in this conversation.