Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 how to get route action name?

Tags:

I'm trying to get the current route action, but I'm not sure how to go about it. In Laravel 4 I was using Route::currentRouteAction() but now it's a bit different.

I'm trying to do Route::getActionName() in my controller but it keeps giving me method not found.

<?php namespace App\Http\Controllers;  use Route;  class HomeController extends Controller {     public function getIndex()     {         echo 'getIndex';         echo Route::getActionName();     } } 
like image 565
Rob Avatar asked Nov 10 '14 09:11

Rob


People also ask

How do I get the route name in blade?

Example 1: Get current route name in Blade Files $route = Route::current(); dd($route); $name = $route->getName(); dd($name); $actionName = $route->getActionName(); dd($actionName); $name = Route::currentRouteName(); dd($name); $action = Route::currentRouteAction(); dd($action);

What is route name?

Named RoutesNamed routes allow the convenient generation of URLs or redirects for specific routes. You may specify a name for a route by chaining the name method onto the route definition: Route::get('/user/profile', function () {


2 Answers

To get action name, you need to use:

echo Route::getCurrentRoute()->getActionName(); 

and not

echo Route::getActionName(); 
like image 83
Marcin Nabiałek Avatar answered Sep 20 '22 18:09

Marcin Nabiałek


In Laravel 5 you should be using Method or Constructor injection. This will do what you want:

<?php namespace App\Http\Controllers;  use Illuminate\Routing\Route;  class HomeController extends Controller {     public function getIndex(Route $route)     {         echo 'getIndex';         echo $route->getActionName();     } } 
like image 36
Laurence Avatar answered Sep 21 '22 18:09

Laurence