Skip to content

Instantly share code, notes, and snippets.

@ziadoz
Last active May 20, 2018 04:52
Show Gist options
  • Save ziadoz/7323919 to your computer and use it in GitHub Desktop.
Save ziadoz/7323919 to your computer and use it in GitHub Desktop.
Simple Silex Controllers
<?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