Esempio n. 1
0
function url_for($url, $absolute = false)
{
    $url = rtRoute::generate($url);
    if ($absolute) {
        $absolute_url = rtRequest::getInstance()->getUri();
        $url = "{$absolute_url}{$url}";
    }
    return $url;
}
Esempio n. 2
0
 public static function loadRoute()
 {
     $array = sfYaml::load(rtConfig::get('rt_app_config_dir') . DS . 'routing.yml');
     $routes = array();
     foreach ($array as $name => $route) {
         $params = isset($route['params']) ? $route['params'] : NULL;
         $routes[] = array($name, $route['url'], $params);
         // formated route
         $routes[] = array("{$name}_formatted", $route['url'] . '.:format', $params);
     }
     rtRoute::connect($routes);
 }
Esempio n. 3
0
 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
 }
Esempio n. 4
0
 /**
  * Compile the config route to regex match
  *
  * @param string $route
  * @return array regex
  */
 private static function compile_route($route)
 {
     self::$compile_tokens = array();
     // internel call back function for regex replacement
     if (!function_exists('_callback')) {
         function _callback($match)
         {
             rtRoute::$compile_tokens[] = $match[1];
             //var_dump(Router::$compile_tokens);echo "<br />";
             return "([a-zA-Z0-9\\-_%]+)";
         }
     }
     $compile_tokens = array();
     // Strip regex chars: / + ? . and _ -
     $reg_strip_parten = "/([\\/\\._-])/";
     $reg_strip_repalce = '\\\\$1';
     // replace with \(regex char)
     $route = preg_replace($reg_strip_parten, $reg_strip_repalce, $route);
     // FIXME more characters should allowed in match parten
     $route = preg_replace_callback("/:([a-zA-Z0-9\\-_%]+)/", '_callback', $route);
     $route = "/^{$route}\$/";
     // add regex slash
     //		echo $route . "<br />";
     return array($route, self::$compile_tokens);
 }