Hi
Today we will learn how to check or get current url or route.
Sometimes we need to check current URL or route and do some action.
1. Check if URL = myurl
Simply – we need to check if URL is exactly like myurl and then we show something.
In Controller:
if (\Request::is('myurl')) {
// do some thing
}
In Blade file – almost identical:
@if (\Request::is('companies'))
// do some thing
@endif
2. Check if URL contains myurl
A little more complicated example – method Request::is() allows a pattern parameter, like this:
if (\Request::is('myurl/*')) {
// will match URL /myurl/999 or /myurl/create
}
3. Check route by its name
As we know, every route can be assigned to a name, in routes.php file it looks something like this:
Route::get('/myroute', ['as' => 'myrt', function () {
return view('myroute');
}]);
So we can check if current route is myrt
if (\Route::current()->getName() == 'myrt') {
// We are on a correct route!
}
So these are three ways to check current URL or route.
Thanks
Today we will learn how to check or get current url or route.
Sometimes we need to check current URL or route and do some action.
1. Check if URL = myurl
Simply – we need to check if URL is exactly like myurl and then we show something.
In Controller:
if (\Request::is('myurl')) {
// do some thing
}
In Blade file – almost identical:
@if (\Request::is('companies'))
// do some thing
@endif
2. Check if URL contains myurl
A little more complicated example – method Request::is() allows a pattern parameter, like this:
if (\Request::is('myurl/*')) {
// will match URL /myurl/999 or /myurl/create
}
3. Check route by its name
As we know, every route can be assigned to a name, in routes.php file it looks something like this:
Route::get('/myroute', ['as' => 'myrt', function () {
return view('myroute');
}]);
So we can check if current route is myrt
if (\Route::current()->getName() == 'myrt') {
// We are on a correct route!
}
So these are three ways to check current URL or route.
Thanks