Today, We want to share with you laravel get current url.In this post we will show you get current url in laravel 6, laravel 7 and laravel 8 application., hear for Laravel – How to get current URL in controller or view? we will give you demo and example for implement.In this post, we will learn about Laravel 5.8 get current url with parameters with an example.
How to get current URL or path in Blade?
Example 1: Get the current path
@if (Request::path() == '/post-title') // Do something @endif
Example 2: Compare the current URL to a pattern
@if (Request::is('apirest/*')) // Do something @endif
Example 3: Get only a segment of the current URL
// Returns true for domainnmae.com/apirest/post and false for domainnmae.com/apirest/edit @if (Request::segment(2) == 'post') // Do something @endif
Example 4: Get the URL without query string
@if (Request::url() == 'some string') // Do something @endforeach
Example 5: Get the full URL, including query string
@if (str_contains(Request::fullUrl(), 'some-string')) // Do something @endif
Get Current Route in Laravel:
$route = Route::current()->getName(); dd($route);
Get Previous URL in Laravel:
$url = url()->previous(); dd($url);
Example : using Request
$currentURL = Request::url(); dd($currentURL);
using full() with Facade(with query string parameters)
$currentURL = URL::full(); dd($currentURL);
current() with Facade
currentURL = URL::current(); dd($currentURL);
full() with Helper(with query string parameters)
$currentURL = url()->full(); dd($currentURL);
current() with Helper
$currentURL = url()->current(); dd($currentURL);
I hope you get an idea about get current URL anywhere.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.