public function indexAction(mfRequest $request) { $this->title = "What a test£¡"; if ($request->getMethod() == mfRequest::GET) { $this->method = 'sure GET'; } }
public function showAction(mfRequest $request) { mfResponse::getInstance()->setViewClass('mfMarkdownView'); $this->setTemplateDir(MF_CORE_DIR . DS . '..' . DS . 'doc' . DS . 'book'); $page = $request->getParameter('page'); // if(!file_exists($this->getTemplateDir() . DS . $page)) throw new mfException404("Page: $page not found"); $this->setTemplate($page); }
/** * Route the request * @return array array($controller, $action, $params); */ public static function route() { $request = mfRequest::getInstance(); // match the routes foreach (self::$_routes as $route) { list($rule, $tokens) = self::compile_route($route[1]); if (preg_match($rule, $request->getParameter('request_path_info'), $matches) == 1) { // fill the request foreach ($tokens as $i => $token) { $request->setParameter($token, $matches[$i + 1]); } // fill with params if (isset($route[2]) && is_array($route)) { foreach ($route[2] as $key => $value) { $request->setParameter($key, $value); } } mfLogger::log(ROUTE_LOG, "matched route: {$route[1]}"); return; } // Log matching mfLogger::log(ROUTE_LOG, "mismatch route: {$route[1]}"); } // no route match throw new RouterExecption('No route match:' . $_SERVER['REQUEST_URI']); }
public static function dispatch() { // Initial middleware classes $middleware_classes = mfConfig::get('middlewares', array()); $middlewares = array(); foreach ($middleware_classes as $middleware_class) { require_once "middleware/{$middleware_class}.php"; $middlewares[] = new $middleware_class(); } // =========================================== // start process request $request = mfRequest::getInstance(); foreach ($middlewares as $middleware) { if (method_exists($middleware, 'process_request')) { $middleware->process_request($request); } } // end process request // =========================================== // Core Process $controller = $request->getController(); $action = $request->getAction(); $controller_class = ucfirst($controller) . 'Controller'; if (!class_exists($controller_class)) { throw new MfException("Can't find Controller: {$controller_class}"); } $controller = new $controller_class(); if (!$controller instanceof mfController) { throw new MfException("Controller:{$controller_class} must extend from mfController class"); } $controller->execute($action . 'Action', $request); // End Core Process // =========================================== // start process response $response = mfResponse::getInstance(); // response middleware process in the reverse direction foreach (array_reverse($middlewares) as $middleware) { if (method_exists($middleware, 'process_response')) { $middleware->process_response($response); } } // end process response }
/** * Include partial to the view * No need to explict echo the result of partial_include * It behavors like php include, borrowed from symfony * * include_partial("index/apache"); * * @param string $partial * @param array $params */ function include_partial($partial, $params = array()) { $request = mfRequest::getInstance(); $format = $request->getParameter('format', 'html'); $array = explode('/', $partial); $count = count($array); $partial = '_' . $array[$count - 1]; // all partials start with '_' $formated_partial = '_' . $array[$count - 1] . ".{$format}"; unset($array[$count - 1]); // pop up partial file name $path = implode('/', $array); // reconnect to the path // specify certain controller to render if ($path) { $view_path = VIEWS_DIR . DS . $path; } else { $view_path = VIEWS_DIR . DS . $request->getController(); } list($view_tpl, $view_class) = findTemplateFileName($view_path . DS . $partial); $view = new $view_class($view_tpl, $params); // before output ob_start have stop direct output to browse, so we echo but no return string echo $view->display(); }
/** * Get Magic View Vars * * @return array */ public function getMagicViewVars() { $magic_vars = array('mf_flash' => mfFlash::getInstance(), 'mf_request' => mfRequest::getInstance(), 'mf_user' => mfUser::getInstance()); return $magic_vars; }
/** * get request instance * * @return mfRequest */ public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; }
function findTemplateFileName($file_without_ext) { $format = mfRequest::getInstance()->getParameter('format', 'html'); // choose the right view class by the file extension found $associations = mfConfig::get('mf.view.classes', array()); foreach ($associations as $association) { if (file_exists($file = $file_without_ext . ".{$format}." . $association['extension']) || file_exists($file = $file_without_ext . '.' . $association['extension']) || file_exists($file = $file_without_ext . '.html.' . $association['extension'])) { $view_class = $association['class']; break; } } if (!isset($view_class)) { throw new mfException("Can't find template: {$file_without_ext}.{format}.php"); } return array($file, $view_class); }
public function showAction(mfRequest $request) { $this->post = Post::findBySlug($request->getParameter('slug')); }
function exception_handler(Exception $e) { // ob_clean(); ob_start(); echo "<div id=\"msg\">" . $e->getMessage() . "</div>"; echo "<pre>" . $e->getTraceAsString() . "</pre>"; echo "<hr /> Vars:<br /><pre>" . var_dump(mfRequest::getInstance()) . "</pre>"; $content = ob_get_clean(); // findTemplateFileName may throw exception, but PHP5.2.5 can't handle this try { list($file, $view_class) = findTemplateFileName(MF_CORE_DIR . DS . 'default' . DS . 'layout'); $view = new $view_class($file, array('mf_layout_content' => $content)); $view->display(); } catch (Exception $e_in) { echo $content . "<hr />"; echo "<div>" . $e_in->getMessage() . "</div>"; echo "<pre>" . $e_in->getTraceAsString() . "</pre>"; } }