How To Get Route Name In Controller Laravel 6?

Laravel 6 , Laravel

Nicesnippets

3100

03-12-2019


Hi Developer,

In this tutorial,i will tell you how can you get your current route name in Laravel 6. we will present route name in controller and view file in laravel 6, it can get current route then "Route" facade of currentRouteName function.i can get current route name laravel 6 so the do the following example.

I can blow you can easily get current route name in this example.

Route

Route::get('home', 'HomeController@index')->name('home');

Example:1

If you are getting route name in controller then "Request" facade will help to get route name.

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routeName = Request::route()->getName();

dd($routeName);

}

Output:

home

Example:2

If you want to get route name in view file or anywhere then you can simply use "Route" facade in currentRouteName function.

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routeName = Route::currentRouteName();

dd($routeName);

}

Output:

home

Example:3

If you want to get route name in view file or anywhere then you can simply use "Route" facade in current function object

/**

* Show the application dashboard.

*

* @return \Illuminate\Contracts\Support\Renderable

*/

public function index()

{

$routeName = Route::current();

dd($routeName->action['as']);

}

Output:

home

I hope it can help you..


Recommended Posts