/** * @param string * @param array * @return void */ public static function render($action, $params = 0) { self::$has_rendered = true; # promote params to local scope if ($params) { foreach ($params as $k => $v) { if ($k != 'controller' && $k != 'action') { eval("\${$k} =& \$params['{$k}'];"); } } } # promote vars to local scope $controller = MVCRouter::$controller; # promote custom controller properties foreach ($controller->attributes as $k => $v) { eval("\${$k} =& \$controller->attributes['{$k}'];"); } # get file if (isset($params['controller'])) { $file = 'app/views/' . $params['controller'] . '/' . $action . '.phtml'; } else { $file = 'app/views/' . $controller->name . '/' . $action . '.phtml'; } if (!@(include APPLICATION_DIR . $file)) { if (!file_exists($file)) { throw new MVCTemplateNotFoundException("Missing template {$file}"); } else { throw new IOException("Failed to access template {$file}"); } } }
/** * @param string * @return void */ function render_text($text) { MVCTemplate::$has_rendered = true; print $text; }
/** * @return void */ public function execute() { if (!isset($_REQUEST['controller'])) { throw new MVCRouterException('Missing controller'); } # get controller name $controller_name =& $_REQUEST['controller']; if (strpos($controller_name, '_') !== false) { $controller_name = strtolower($controller_name); } elseif (ctype_upper($controller_name[0])) { $controller_name = Inflector::underscore($controller_name); } # get controller class $controller_class = Inflector::camelize($controller_name) . 'Controller'; # require app controller if (!@(include_once APPLICATION_DIR . 'app/controllers/application.php')) { error_log("No application controller found! Looked for " . APPLICATION_DIR . 'app/controllers/application.php'); throw new MVCRouterException('No application controller found!'); } # load controller if (!@(include_once APPLICATION_DIR . 'app/controllers/' . $controller_name . '_controller.php')) { throw new MVCRouterException("Can not find controller '{$controller_name}'"); } # instantiate controller $controller = new $controller_class(); MVCRouter::$controller = $controller; # set controller properties $controller->name = $controller_name; $controller->action_name = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'index'; # execute action ob_start(); try { $_action = $controller->action_name; $controller->{$_action}; #call_user_func(array($controller, $controller->action_name)); # render template (if not allready explicitly rendered by controller) if (!MVCTemplate::$has_rendered) { # we want MVCTemplate to use MVCRouter::$controller instead of controller from request if (isset($_REQUEST['controller'])) { unset($_REQUEST['controller']); } MVCTemplate::render($controller->action_name, $_REQUEST); } # finalize response $content_for_layout = ob_get_clean(); if (!@(include APPLICATION_DIR . 'app/views/layouts/' . $controller->name . '.phtml')) { # Missing layout for controller - render content directly print $content_for_layout; } } catch (Exception $e) { ob_end_clean(); throw $e; } }