Created
March 31, 2015 13:44
-
-
Save plweil/edf741f0dc2cf4ec43ed to your computer and use it in GitHub Desktop.
Laravel 5 Class method for returning names of current controller (minus the string 'Controller'), and method (action) from current route.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http; | |
use Illuminate\Routing\Route; | |
class RouteHelper { | |
/** | |
* Get and return names of current controller, minus the string 'Controller', and method (action) from current route. | |
* To use, enter the following into your controller method: | |
* list($controller, $action) = RouteHelper::controller_action($route); | |
* | |
* @param Route $route | |
* | |
* @return array | |
*/ | |
public static function controller_action(Route $route) | |
{ | |
$action =$route->getActionName(); | |
$action = explode('\\', strtolower($action)); | |
$controller_and_action = explode('@', end($action)); | |
$controller_and_action[0] = substr_replace($controller_and_action[0], '', -10); | |
return $controller_and_action; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This method returns the (shortened) controller and action names to use as classes, etc. This is the simplest way I've found to do this thus far.