How to Get Current Route Name in Laravel 10 Tutorial

Reading Time: 4 minutes
546 Views

When you need to execute actions based on the active route, retrieving the current route name in Laravel 10 is a typical task. With real examples, we will walk you through the process of getting the current route name in Laravel 10.

Laravel’s routing system makes it simple to design and manage routes, and you may need to know which route is presently being accessed for a variety of reasons, such as navigation, conditional logic, or link generation.

Read More: How To Upload File with Progress Bar in Laravel 10 Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

What are Named Routes in Laravel?

Named routes in Laravel are a technique to give a unique and memorable name to a certain route in your application. Instead of referring to routes by their URIs (Uniform Resource Identifiers) or controller action names, you can utilise named routes to conveniently construct URLs or redirect to routes.

Named routes are especially useful in large applications where managing routes becomes difficult. A named route can be defined by include a name when defining your routes in the routes/web.php or routes/api.php files.

In Laravel, you can define and use named routes like follows:

Defining Named Routes

Route::get('/about', 'PageController@about')->name('about');

In this example, we’ve created a named route called “about” for the /about URI, which is linked to the PageController@about controller action.

How To Get Route Name in Controller File?

You can use Request object to get the current route name,

$request->route()->getName();

Here, you will see how to get the current route name in any controller file of your application.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PageController extends Controller
{
    public function about(Request $request)
    {
        $routeName = $request->route()->getName();
      
        dd($routeName);
    }
}

For /about application route it will return about route name.

How To Get Route Name in Blade Template File?

For blade template files you have two options available,

Read More: How To Handle Exception in Laravel 10 Example Tutorial

Let’s consider the same route /about for this case,

Example #1

<p>{{ Route::currentRouteName() }}</p>

Output

about

Example #2

<p>{{ Route::getCurrentRoute()->getActionName(); }}</p>

Output

App\Http\Controllers\PageController@about

How To Get Route Name in Middleware File?

Here, in this case if want to access the route name in middlewares then you can use it like this.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CustomMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $routeName = $request->route()->getName();
        
        dd($routeName);
      
        return $next($request);
    }
}

Concept,

$routeName = $request->route()->getName();

That’s it.

We hope this article helped you to learn about How to Get Current Route Name in Laravel 10 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.