
This tutorial will give you details on how to check current url or route in your laravel project, many time we needs to check current request url into our controller to do some action based on it.
Here will show you few different example of checking current requested urls or routes in laravel, this is very useful when it comes to play with menus or blade templates in laravel projects.
I assume that you already have laravel project up and running if not you checkout this tutorial to setup new laravel 6 application.
Follow – Develop Exclusive Laravel 6 CRUD Application with Input Validations
How to Get Current Request URL
Get current URL without the query string
echo \Request::url();
Or use global url function
echo url()->url();
Or use current function
echo url()->current();
Get current full url with query string parameters
echo url()->full();
Get previous URL in Laravel
Sometime we need to check what was the last requested or previous url, it is simple to do in laravel here is how
echo url()->previous();
Current URL is Equal to given String
To evaluate or hide or show something depending on the requested we need to match that the current url is equal to given string we can do that using is function from the request facade.
if (request()->is('user-profile')) {
//
}
Current URL Contains given Pattern
We can also check url using particular pattern like if you want to check that url matches to certain pattern product/3939
Here is the example to achieve this using same request facade.
if (request()->is('product/*')) {
//
}
How to Get Route by Name
If you need to check the name of the current route we can do that use Route class, it is helpful when you don’t want to play with full url or your url might change in future.
\Route::current()->getName();
Match current route using route Name
As we see in the above step we can route by it’s name we can easily check current url using the route name simply do this:
if(\Route::current()->getName() == 'name-of-your-dsire-route')
{
}
window.setInterval(function(){
var v = document.getElementById(‘ab-message’);
if(v){ v.remove(); }
}, 1);