Skip to content

Instantly share code, notes, and snippets.

@tleilax
Created February 28, 2012 07:47
Show Gist options
  • Save tleilax/1930439 to your computer and use it in GitHub Desktop.
Save tleilax/1930439 to your computer and use it in GitHub Desktop.
Simple dispatcher
function dispatcher($controller = null, $method = null)
{
$controller = $controller ?: $_REQUEST['controller'];
$method = $method ?: $_REQUEST['method'] ?: 'index';
$class = $controller . 'Controller';
$action = $method . '_action';
if (!class_exists($class)) {
require 'controllers/' . $class . '.php';
}
if (!class_exists($class)) {
throw new BadMethodCallException('Unknown controller "' . $controller . '" called');
}
$instance = new $class;
if (!is_a($instance, 'Controller')) {
throw new BadMethodCallException('Invalid controller "' . $controller . '" called');
}
if (!method_exists($instance, $action)) {
throw new BadMethodCallException^('Unknown action "' . $method . '" called on controller "' . $controller . '"');
}
$class->$action();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment