Skip to content

Instantly share code, notes, and snippets.

@brihter
Created September 17, 2014 11:43
Show Gist options
  • Save brihter/359111d0ac96c3b7d9d4 to your computer and use it in GitHub Desktop.
Save brihter/359111d0ac96c3b7d9d4 to your computer and use it in GitHub Desktop.
Call Laravel's controller actions automatically, no setup required.
<?php
// app/routes.php
Route::get('/{controller}/{action?}/{params?}', function($controller, $action = 'index', $params = '')
{
// controller
$controller = strtolower($controller);
$controller = ucfirst($controller);
$controller .= 'Controller';
// params
$args = array();
if (strlen($params) > 0) {
$args = explode('/', $params);
}
// go
$controllerInstance = App::make($controller);
$reflectionMethod = new ReflectionMethod($controller, $action);
$reflectionMethod->invokeArgs($controllerInstance, $args);
})->where('params','.*');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment