Getting the controller and action in Laravel 4
This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.
In my old CodeIgniter days, it was really easy to get the current controller and action for a request. Of course, it was a little bit easier becauce in CodeIgniter every request is routed to a controller action. Here is an excerpt from Studious when it was running on CodeIgniter:
This simply put a class like post-show
on the body tag, which could be used for custom styling or script hooks.
In Laravel 4, I haven't quite found a solution that totally satisfies me, but I'll share what I'm using at the moment. It makes use of Route::currentRouteAction()
and Str::parseCallback()
which you should look into if you want to write up your own solution.
I've put this up as a function on Gist, note that I've used the PHP 5.4 syntax there. You might prefer to extend the existion Route
class to add the function, or use a separate Helper
class as I've opted to above.