Last active
May 20, 2018 04:52
-
-
Save ziadoz/7323919 to your computer and use it in GitHub Desktop.
Simple Silex Controllers
This file contains 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 | |
// Function. | |
function controller($class) { | |
$args = func_get_args(); | |
$class = array_shift($args); | |
return function() use ($class, $args) { | |
if (count($args) > 0) { | |
$reflection = new ReflectionClass($class); | |
return $reflection->newInstanceArgs($args); | |
} | |
return new $controller; | |
}; | |
} | |
// Controllers. | |
class FooController | |
{ | |
public function indexAction() | |
{ | |
return 'Foo'; | |
} | |
} | |
class BarController | |
{ | |
public function __construct() | |
{ | |
print_r(func_get_args()); | |
} | |
public function indexAction() | |
{ | |
return 'Bar'; | |
} | |
} | |
// Silex. | |
$app = new Silex\Application; | |
$app['controller.foo'] = $app->share(controller('FooController')); | |
$app['controller.bar'] = $app->share(controller('BarController', 'param', 'param')); | |
$app->get('/foo', 'controller.foo:indexAction'); | |
$app->get('/bar', 'controller.bar:indexAction'); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment