Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dwightwatson
Last active June 6, 2016 00:07
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dwightwatson/6200599 to your computer and use it in GitHub Desktop.
Save dwightwatson/6200599 to your computer and use it in GitHub Desktop.
Get the controller name and action in Laravel 4 which can be used as a class in your HTML. This solution makes use of the parseCallback() and currentRouteAction() functions, which are handy if you want to devise your own version.
<?php
public function routeClass()
{
$routeArray = Str::parseCallback(Route::currentRouteAction(), null);
if (last($routeArray) != null) {
// Remove 'controller' from the controller name.
$controller = str_replace('Controller', '', class_basename(head($routeArray)));
// Take out the method from the action.
$action = str_replace(['get', 'post', 'patch', 'put', 'delete'], '', last($routeArray));
return Str::slug($controller . '-' . $action);
}
return 'closure';
}
@vienhoang
Copy link

Thanks man! :)

@jrsalunga
Copy link

where do you put this in your code and how it use?

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