public function addAction(rtRequest $request) { if ($request->isPost()) { $c = new Catagory(); $c->name = $request->getParameter('name'); $c->save(); $this->setFlash('notice', '成功添加分类'); $this->redirect("blog/index"); } }
/** * Route the request * @return array array($controller, $action, $params); */ public static function route() { $request = rtRequest::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); } } rtLogger::log(ROUTE_LOG, "matched route: {$route[1]}"); return; } // Log matching rtLogger::log(ROUTE_LOG, "mismatch route: {$route[1]}"); } // no route match throw new RouterExecption('No route match:' . $_SERVER['REQUEST_URI']); }
function url_for($url, $absolute = false) { $url = rtRoute::generate($url); if ($absolute) { $absolute_url = rtRequest::getInstance()->getUri(); $url = "{$absolute_url}{$url}"; } return $url; }
public static function dispatch(rtAppConfig $config) { // Load the project's config Rythm::init(); // Load the application's config // Route the request rtRoute::route(); //============================================================ // Start Handle the request //============================================================ // Initial middleware classes $middleware_classes = rtConfig::get('middlewares', array()); $middlewares = array(); foreach ($middleware_classes as $middleware_class) { require_once "middleware/{$middleware_class}.php"; $middlewares[] = new $middleware_class(); } // =========================================== // middleware start process request $request = rtRequest::getInstance(); foreach ($middlewares as $middleware) { if (method_exists($middleware, 'process_request')) { $middleware->process_request($request); } } // middleware end process request // =========================================== // Core Process $controller = $request->getController(); $action = $request->getAction(); $controller_class = ucfirst($controller) . 'Controller'; if (!class_exists($controller_class)) { throw new rtException("Can't find Controller: {$controller_class}"); } $controller = new $controller_class(); if (!$controller instanceof rtController) { throw new rtException("Controller:{$controller_class} must extend from rtController class"); } $controller->execute($action . 'Action', $request); // End Core Process // =========================================== // start process response $response = rtResponse::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 = rtRequest::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 = rtConfig::get('rt_app_views_dir') . DS . $path; } else { $view_path = rtConfig::get('rt_app_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(); }
public function showAction(rtRequest $request) { $table = Doctrine::getTable('Content'); $this->post = $table->findOneBySlug($request->getParameter('slug')); }
function findTemplateFileName($file_without_ext) { $format = rtRequest::getInstance()->getParameter('format', 'html'); // choose the right view class by the file extension found $associations = rtConfig::get('rt.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 rtException("Can't find template: {$file_without_ext}.{format}.php"); } return array($file, $view_class); }
/** * Get Magic View Vars * * @return array */ public function getMagicViewVars() { $magic_vars = array('rt_flash' => rtFlash::getInstance(), 'rt_request' => rtRequest::getInstance(), 'rt_user' => rtUser::getInstance()); return $magic_vars; }
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(rtRequest::getInstance()) . "</pre>"; $content = ob_get_clean(); // findTemplateFileName may throw exception, but PHP5.2.5 can't handle this try { list($file, $view_class) = findTemplateFileName(RT_CORE_DIR . DS . 'default' . DS . 'layout'); $view = new $view_class($file, array('rt_layout_content' => $content)); $view->display(); } catch (Exception $e_in) { echo $content . "<hr />"; echo "<div>" . $e_in->getMessage() . "</div>"; echo "<pre>" . $e_in->getTraceAsString() . "</pre>"; } }
/** * get request instance * * @return rtRequest */ public static function getInstance() { if (!self::$_instance) { self::$_instance = new self(); } return self::$_instance; }