Skip to content

Instantly share code, notes, and snippets.

@mayoralito
Created June 4, 2014 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayoralito/0d4a6f3bc629a62227c7 to your computer and use it in GitHub Desktop.
Save mayoralito/0d4a6f3bc629a62227c7 to your computer and use it in GitHub Desktop.
<?php
/**
* Copyright (c) 2013 Roman Ožana (http://omdesign.cz)
*
* For the full copyright and license information, please view
* the file license.txt that was distributed with this source code.
*
* @author Roman Ozana <ozana@omdesign.cz>
* @method Homepage()
* @method Homepage_actiom()
*/
class App extends Slim {
public function __construct() {
parent::__construct();
$this->container->singleton(
'mysuperclass', function ($container) {
return new MySuperClass();
}
);
}
public function __call($name, $params) {
return function () use ($name, $params) {
list($class, $action) = explode('_', $name . '_handle'); // default method is handle
$args = [];
$class = new \ReflectionClass($class);
$constructor = $class->getConstructor();
foreach ($constructor->getParameters() as $param) {
$args[] = ($param->name === 'app') ? $this : $this->container->get($param->name);
}
$controller = $class->newInstanceArgs($args);
return call_user_func([$controller, $action], func_get_args() + $params);
};
}
}
<?php
class Homepage {
public $app;
public $router;
public $mysuperclass;
public function __construct(\App $app, \Slim\Router $router, \MySuperClass $mysuperclass) {
$this->app = $app;
$this->router = $router;
$this->mysuperclass = $mysuperclass;
}
public function handle() {
var_dump($this->app); // have something
}
public function action() {
// call action
}
}
<?php
$app = new App();
$app->map('/', $app->Homepage())->via('GET', 'POST');
$app->map('/action', $app->Homepage_action())->via('GET', 'POST');
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment