Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
controller loader for slim framework
<?php
Slim::hook('slim.before.dispatch', function () {
list( $type, $controller_name, $controller_action ) = array_merge( explode( '.', Slim::router()->current()->getName() ), array( null,null,null ) );
if( $type == 'controller' ) {
$file = './controllers/'.$controller_name.'.controller.class.php';
if( !is_readable( $file ) ) { return false; }
require_once( './controllers/base.controller.class.php' );
require_once( $file );
$class = $controller_name.'Controller';
$controller = new $class( Slim::getInstance() );
$params = Slim::router()->current()->getParams();
$action = ( isset( $params['action'] ) && is_callable( array( $controller, $params['action'] ) ) ) ? $params['action'] : 'index';
$controller->$action();
}
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment