How to Get Current Route Name Path and Action in Laravel?

10-Apr-2023

.

Admin

Hello Developer,

In this short tutorial, we will cover a How to get current route name path and action in laravel. Here you will learn how to get current route name in laravel blade. you can understand a concept of how to get current route name in laravel. We will use laravel get route name. follow the below example for laravel get route name in controller.

In this post, i will tell you how can you get your current route name, route path and route action name in Laravel 6 and also you can get all registered route paths in Laravel.

Let's assume, i have created a resource route with testCRUD and navigating its index method then what will be response if i will use following methods to get information about routes.

How to get current route name in Laravel


/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routes = \Request::route()->getName();

dd($routes);

}

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routes = \Route::currentRouteName();

dd($routes);

}

How to get current route path in Laravel

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routes = \Route::getCurrentRoute()->getPath();

dd($routes);

}

How to get current route action in Laravel

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routes = \Route::getCurrentRoute()->getActionName();

dd($routes);

}

How to get a list of all routes in Laravel

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routes = \Route::getRoutes();

foreach ($routes as $route) {

echo $route->getPath();

}

}

It will help you...

#Laravel

#Laravel 6