Created
February 28, 2012 07:47
-
-
Save tleilax/1930439 to your computer and use it in GitHub Desktop.
Simple dispatcher
This file contains 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
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