Skip to content

Instantly share code, notes, and snippets.

@plweil
Created March 31, 2015 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plweil/edf741f0dc2cf4ec43ed to your computer and use it in GitHub Desktop.
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.
<?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;
}
}
@plweil
Copy link
Author

plweil commented Mar 31, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment