Laravel 9 How to Get Current Route Name Tutorial

Share this Article
Reading Time: 4 minutes
291 Views

Inside this article we will see the concept i.e Laravel 9 How To get current route name. Additionally we also see how to set name to laravel route. Article contains classified information about getting route name in laravel 9.

If you are looking for a concept to get route name inside laravel controller, views, middlewares, etc then this article will help you a lot for this.

Route is a way of creating a request URL of your application. These URLs do not have to map to specific files on a website. The best thing about these URLs is that they are both human readable and SEO friendly. In laravel we define routes inside web.php file of /routes folder.

Learn More –

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.

How To Set Route Name

To set a name to a route, the very basic syntax is:

Route::get("route name", [Controller::methodName])->name("route name");

Route::post("route name", [Controller::methodName])->name("route name");

...

Example

Route::get('/post', [PostController::class, 'getData'])->name("post.index");

Now, let’s see if you have routes with name then how you will be able to get that.

Get Route Name in Controller File

Here,

We will see how to get route name in laravel controller file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

Concept

You can use the request object to get route name.

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

Output

"post.index"

Get Route in Blade File

Here,

We will see how to get route name in laravel blade template files. For blade template files we have two options available.

Example #1

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

Output

post.index

Example #2

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

Output

App\Http\Controllers\PostController@index

Get Route in Middleware File

Here,

In this case if want to access the route name in middlewares then we can use it like this.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

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

This is the line of concept to understand –

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

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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5