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

muragijimana's avatar

How to get Current url path

I have the path like localhost:8000/home

how can i get this path in code exactly as it is so that i can do echo and get the same result.

0 likes
6 replies
orrd's avatar

Another option, if you have the "Request $request" object (which you probably do if you used dependency injection in the controller), then you can just do this:

$request->url()

Note that that's the URL without any query (GET) variables. These are also useful depending on what you want:

// Full URL, with query string
$request->fullUrl()

// Just the path part of the URL 
$request->path()

// Just the root (protocol and domain) part of the URL)
$request->root()

Those methods also work from the "Request::" facade.

10 likes
Mego's avatar

Guys, is there available another methods on function url()? Or just current()? It's because I need to get almost full URL, but without ending {slug} and I don't know how.

inforkgodara's avatar

I am not aware of where do you want to get current path at server side or client side. here I am giving you both ways to get current url in laravel application.

  1. Server side use Illuminate\Support\Facades\URL;

echo URL::current();

  1. Client side You can use url() helper in laravel blade url()->current()

  2. Client side in Javascript document.URL;

I hope reply will be helpful you. It is quite late. but it will help to new comers.

4 likes

Please or to participate in this conversation.