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!'); } }
/** * Combines layout and view(s) and prints final output * * @return string * @author Charles Mastin * @author Joshua Rudd **/ public function render() { $r = null; // Create variables for layout from layout_data array if ($this->layout_data) { foreach ($this->layout_data as $dataKey => $dataValue) { ${$dataKey} = $dataValue; } } // Create content variables from container output array $content = array(); if ($this->output) { foreach ($this->output as $dataKey => $dataValue) { $content[$dataKey] = $dataValue; } } // Capture output of layout/view and then print it ob_start(); // If no layout is set, just spit out the view(s) if (!$this->layout_view) { if (isset($content['main'])) { $r .= $content['main']; } } elseif ($this->layout_view && @(include $this->layout_file)) { $r .= ob_get_contents(); } else { //ErrorHandler::code = 404; ErrorHandler::message('The ' . $this->layout_view . '.tpl layout could not be found!'); } ob_end_clean(); return $r; }