get current path Laravel 5

19,448

Solution 1

you can use

Request::url()

blade:

{{\Request::url()}} // if is in blade

Solution 2

Laravel has a handy method on the Request class, coincidentally, called fullUrl():

Request::fullUrl();

This is covered in the Laravel documentation: https://laravel.com/docs/master/requests#accessing-the-request

Solution 3

Try this clause:

URL::current();

Or:

$request->url();

Or PHP way:

$_SERVER['REQUEST_URI'];
Share:
19,448
Jos Koomen
Author by

Jos Koomen

Creative Developer from the netherlands

Updated on June 10, 2022

Comments

  • Jos Koomen
    Jos Koomen almost 2 years

    I've tried to find the full path of current route in Laravel 5.x

    For this case i've created a method with the following code, but i can't imagine that Laravel does not provide something like this themselves:

     $current = Route::getFacadeRoot()->current();
     $uri = $current->uri();
     foreach ($current->parameters() as $key => $param) {
         $uri = str_replace('{' . $key . '}', $param, $uri);
     }
     return url($uri);
    

    Is there something out of the box in Laravel that i just can't find?

  • Jos Koomen
    Jos Koomen about 8 years
    Yep, that's working! Thanks Request::fullUrl(); does also pass the GET request parameters which i just don't need. In that case the answer of @kscorrales is working for my own situation.
  • Jos Koomen
    Jos Koomen about 8 years
    Thanks, this was my solution.. use Illuminate\Http\Request; and you need an instance of Request. the url added by @martin-bean explains how