public static function dispatch($routes) { $output = ''; // Find a matching route if ($route = self::findRoute($routes)) { self::$route = $route; //format the file name of the controller - camel case, append Controlller $name = ucfirst($route['controller']) . 'Controller'; $file = CONTROLLERS . $name . '.php'; if (file_exists($file) && (require_once $file)) { //action $action = DEFAULT_ACTION; if (isset($route['action'])) { $action = $route['action']; } $action = ucfirst($action); $controller = new $name($route); //could force index to always exist by using an interface or abstract class //however, this needs to be here to catch human error in a route if (method_exists($controller, $action)) { $controller->{$action}(); $output .= $controller->render(); if (function_exists('plugin__pre_render')) { $output = plugin__pre_render($output); } print $output; if (function_exists('plugin__post_render')) { plugin__post_render($output); } //stop matching return; } else { //action doesn't exist //self::$error->code = 404; ErrorHandler::message($action . ' action doesn\'t exist in ' . $name); } } else { //self::$error->code = 404; ErrorHandler::message($name . ' controller doesn\'t exist'); } } else { //self::$error->code = 404; //ErrorHandler::message('Router did not match any controllers'); ErrorHandler::message('No valid route found!'); } }