In this tutorial, We will learn how to get current route name in laravel. will get the route name in controller and blade file. We will explain some methods in laravel get current route name.
You can use that route name wherever like controller, blade file etc. You need to use route() function and pass the route name and argument in that function and return the route using that route name. You can easily check route in that method.
We have explained various example to get current route name in laravel. You can see below example.
Following method to get current route name Laravel.
- Laravel get current route name using request.
- Laravel get current route name using route
- Laravel get current route name using route facades
1. Laravel get current route name using request:
$getRouteName = Request::route()->getName();
dd($getRouteName);
Explanation:
- This is essentially the same as Route::currentRouteName() but accessed throught the request helper. It is often useful middleware or controller where you may already have a request instance.
2. Laravel get current route name using route:
$getRouteName = Route::getCurrentRoute()->getPath();
dd($getRouteName);
3. Laravel get current route name using route facades
You can use Route Facades to access information about their names.Route facade provides a variant of method for interacting with route.can use the getName() method. let’see below example.
First include the Route facade to controller.
use Illuminate\Support\Facedes\Route;
$getRouteName = \Route::currentRouteName();
$getRouteName = Route::getRoutes()->match($request()->getName());
You can use this code in your use to retrieve the name of the current route or specific route based on the URL. import the Route facade at the top of your file if you haven’t already done so.
Read also: How to Create Seeder in Laravel
4. Get route name in blade view
You can directly laravel get route name in blade file. To do so, you could use the ‘Route’ facade directly blade file.
{{ Route::currentRouteName() }}
Or if you want to get your URI, you could also use the ‘requests’ function directly:
{{ request()->route()->uri }}
5. You can getting additional information about your route
You wanted to get all of the information about the route you can use the ‘current’ method:
public function getRouteInformation()
{
$routeInformation = \Route::current();
dd($routeInformation);
}
6. Laravel check route name
This functionality is commonly required in design element such as the header or navigation, where it becomes essential to determine the active route in order to highlight the corresponding link.
<div class="flex justify-between">
<a class="text-gray underline" href="/">Home</a>
<a class="text-gray underline" href="about">About</a>
<a class="text-gray underline" href="contact">Contact</a>
</div>
We have a basic header, and we want to change it so that those underline classes only apply when the link in the header active one for the user. Let’ see example below.
<div class="flex justify-between">
<a @class=(['text-gray ', 'underline' => Route::is('/')]) href="/">Home</a>
<a @class=(['text-gray ', 'underline' => Route::is('about')]) href="/about">About</a>
<a @class=(['text-gray ', 'underline' => Route::is('contact')]) href="/contact">Contact</a>
</div>
You can provide a regular expression to the Route::is method. You can easily check specific route all subroute.
<div class="flex justify-between">
<a @class(['text-gray ', 'underline' => Route::is('items.*')])
// web.php
Route::resource('items', ItemController::class);
</div>
Now, the underline class will be applied when any route name, starting with items. is active. In the routes above, you can see that both route names start with product., meaning despite the inconsistency in URL structure – /items/*, they will both match.
7. Retrieving the request URI
The method returns the request’s URI. So, if the incoming request is targeted at url, method will return only path.
$uri = $request->path();
This can be useful for simple checks without the full URL.
8. Redirect based on route name
Laravel get current route name with check current route. You can redirect users based on the current route. Let’see below example.
if(Route::currentRouteName() == 'login-page') {
return redirect()->route('dashboard');
}
9. Check current route name in middleware
In this example explain you can check laravel check current route and apply specific condition in middleware.
public function handle($request, Closure $next) {
if($request->route()->getName() == 'user') {
return redirect('dashboard');
}
return $next($request);
}
If you want to select menu on multiple routes you may do like this.
<li class="{{Request::is('descriptions/*') || Request::is('descriptions') || Request::is('descriptions/*') ? 'active' : ''}}"><a href="{{url('descriptions')}}">descriptions</a></li>
Or if you want to select just single menu you may do like this.
<li class="{{Request::is('/descriptions') ? 'active' : ''}}"><a href="{{url('descriptions')}}">descriptions</a></li>
I hope this tutorial help you.