Last active
August 29, 2015 14:19
-
-
Save leite/01e275d503ae7a9154d2 to your computer and use it in GitHub Desktop.
trying out slimphp ... managing to get more information out of routes
This file contains hidden or 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 namespace app; | |
/** | |
* | |
*/ | |
use \Slim\App as App; | |
use \Slim\Views\Lightncandy as Lightncandy; | |
define(BLANK, ''); | |
class Bootstrap { | |
function __construct ($boot_app = true) { | |
$env = file_exists('env') ? file_get_contents('env') : 'development'; | |
$config = Spyc::YAMLLoad( __DIR__ .'/config.yaml'); | |
date_default_timezone_set(isset($config['timezone']) ? $config['timezone'] : 'UTC'); | |
define(APP_FOLDER, realpath(__DIR__.'/../app')); | |
define(VIEWS_PATH, realpath(APP_FOLDER .'/../app/views')); | |
define(CONTROLLERS_PATH, realpath(APP_FOLDER .'/../app/controllers')); | |
$this->config = isset($config[$env]) ? $config[$env] : array(); | |
if (!$boot_app) return; | |
$this->app = new App(array( | |
'mode' => $this->opt('mode', 'development'), | |
'debug' => $this->opt('debug', true), | |
'cookies.lifetime' => $this->opt('lifetime', '30 minutes'), | |
'cookies.secret_key' => $this->opt('secret', '184p0') | |
)); | |
$this->app->register(new Lightncandy(VIEWS_PATH, array())); | |
$this->register_controllers(); | |
$this->register_routes(); | |
$this->app->run(); | |
} | |
protected function opt ($value, $value2 = '', $default = '') { | |
if (is_array($value)) | |
return isset($value[$value2]) ? $value[$value2] : $default; | |
$default = $value2; | |
return isset($this->config[$value]) ? $this->config[$value] : $default; | |
} | |
function register_controllers () { | |
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(CONTROLLERS_PATH)); | |
$controllers = new RegexIterator($iterator, '/.*\.php$/', RegexIterator::GET_MATCH); | |
foreach ($controllers as $controller) { | |
$ctrl = str_replace( | |
array(APP_FOLDER, '/', '.php'), array(BLANK, '\\', ''), $controller[0] | |
); | |
$this->app[substr($ctrl, 1)] = function ($app) use ($ctrl) { | |
return new $ctrl($app); | |
}; | |
} | |
} | |
function register_routes () { | |
$app = &$this->app; | |
// usersdoom | |
$app->get('/users', 'controllers\User:index')->setName('users'); | |
// ... a bunch of them ... | |
} | |
} | |
$boot = new Bootstrap(); | |
?> |
This file contains hidden or 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
{ | |
"minimum-stability": "dev", | |
"require": { | |
"slim/slim": "dev-develop", | |
"endel/slim-lightncandy-view": "dev-master", | |
"mustangostang/spyc": "0.5.1" | |
}, | |
"autoload": { | |
"psr-4": { | |
"" : [ "app/" ] | |
} | |
} | |
} |
This file contains hidden or 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
controllers: app/controllers | |
views: app/views | |
development: | |
mode: development | |
debug: true | |
secret: y4mLESArWNqzSsSzUZXS | |
staging: | |
mode: test | |
debug: true | |
secret: vrRfD9uLReDDdxF2zW29 | |
production: | |
mode: production | |
debug: false | |
secret: mrHDgrbkkR9yHVpJtB2H |
This file contains hidden or 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 namespace app/core; | |
/** | |
* core/Controller | |
*/ | |
class Controller { | |
private $app; | |
private $info; | |
function __construct ($instance) { | |
$this->$app = $instance; | |
// get controller name and method, figure it out! | |
$this->info = array( | |
'controller' => '??', | |
'action' => '????' | |
); | |
} | |
function __call ($name, args) { | |
} | |
protected function view ($view, $args = array()) { | |
$this->app['view']->render($file, array_merge($args, $this->info)); | |
} | |
} | |
?> |
This file contains hidden or 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 namespace app/controllers; | |
/** | |
* controllers/User | |
*/ | |
class User extends core\Controller { | |
function __construct ($instance) { | |
parent::__construct($instance); | |
} | |
protected function index ($req, $res) { | |
$this->view('index', array('title' => 'Hallo aus Brazil')); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment