Reputation: 423
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
Reputation: 7992
public function yourMethod(Request $request)
{
return view('your-view', [ 'current-uri' => $request->route()->uri() ]);
}
Upvotes: 1
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
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