Farzan Badakhshan
Farzan Badakhshan

Reputation: 423

Getting current URI in Laravel 5.7

I am aware that in the previous versions of Laravel (e.g. 4), one could get the current uri through

Route::current()->uri();

However, this doesn't seem to work in Laravel 5.7 or later. I am wondering how it should be rewritten. Please note that I am accessing the uri in a blade view and hence cannot use non-static methods.

Upvotes: 9

Views: 21442

Answers (3)

linktoahref
linktoahref

Reputation: 7992

public function yourMethod(Request $request)
{
    return view('your-view', [ 'current-uri' => $request->route()->uri() ]);
}

Documentation

Upvotes: 1

Jigar
Jigar

Reputation: 3281

You can get current url in laravel using folling methods.

// Get the current URL without the query string...
echo url()->current();

// Get the current URL including the query string...
echo url()->full();

// Get the full URL for the previous request...
echo url()->previous();

Each of these methods may also be accessed via the URL facade:

use Illuminate\Support\Facades\URL;

echo URL::current();

For more information you can read full documentation here.

Upvotes: 22

Dwight
Dwight

Reputation: 12470

That should still work - but it may not work as expected if the current path is not named. You should probably instead get the path from the request.

Request::path();

It's probably also checking the API of the request instance as there are a number of related methods you can call on it.

Request::root();
Request::url();
Request::fullUrl();
Request::fullUrlWithQuery();

Upvotes: 6

Related Questions