Exemple #1
0
 /**
  * Extension of the main request factory. If none given, the URI will
  * be automatically detected. If the URI contains no language segment, the user
  * will be redirected to the same URI with the default language prepended.
  * If the URI does contain a language segment, I18n and locale will be set.
  * Also, a cookie with the current language will be set. Finally, the language
  * segment is chopped off the URI and normal request processing continues.
  *
  * @param   string  $uri URI of the request
  * @param   Cache   $cache
  * @param   array   $injected_routes an array of routes to use, for testing
  * @return  Request
  * @uses    Lang::config
  * @uses    Request::detect_uri
  * @uses    Lang::find_current
  * @uses    Lang::$default_prepended
  * @uses    Lang::$default
  * @uses    Request::lang_redirect
  * @uses    Request::$lang
  * @uses    I18n::$lang
  * @uses    Cookie::get
  * @uses    Lang::$cookie
  * @uses    Cookie::set
  */
 public static function factory($uri = TRUE, $client_params = array(), $allow_external = TRUE, $injected_routes = array())
 {
     // Load config
     $config = Lang::config();
     if ($uri === TRUE) {
         // We need the current URI
         $uri = Request::detect_uri();
     }
     // Get current language from URI
     $current_language = Lang::find_current($uri);
     if (!Lang::$default_prepended and $current_language === Lang::$default and strpos($uri, '/' . Lang::$default) === 0) {
         // If default is not prepended and current language is the default,
         // then redirect to the same URI, but with default language removed
         Request::lang_redirect(NULL, ltrim($uri, '/' . Lang::$default));
     } elseif (Lang::$default_prepended and $current_language === Lang::$default and strpos($uri, '/' . Lang::$default) !== 0) {
         // If the current language is the default which needs to be
         // prepended, but it's missing, then redirect to same URI but with
         // language prepended
         Request::lang_redirect($current_language, $uri);
     }
     // Language found in the URI
     Request::$lang = $current_language;
     // Store target language in I18n
     I18n::$lang = $config[Request::$lang]['i18n_code'];
     // Set locale
     setlocale(LC_ALL, $config[Request::$lang]['locale']);
     if (Cookie::get(Lang::$cookie) !== Request::$lang) {
         // Update language cookie if needed
         Cookie::set(Lang::$cookie, Request::$lang);
     }
     if (Lang::$default_prepended or Request::$lang !== Lang::$default) {
         // Remove language from URI if default is prepended or the language is not the default
         $uri = (string) substr($uri, strlen(Request::$lang) + 1);
     }
     // Continue normal request processing with the URI without language
     return parent::factory($uri, $client_params, $allow_external, $injected_routes);
 }
Exemple #2
0
 public static function factory($uri = TRUE, HTTP_Cache $cache = NULL, $injected_routes = array())
 {
     $request = parent::factory($uri, $cache, $injected_routes);
     // for admin routes
     $exluded_routes = array('admin', 'admin_error', 'modules');
     if ($request->route() !== NULL and in_array($request->route()->route_name, $exluded_routes)) {
         return $request;
     }
     // for public routes
     $request->define_site();
     ORM_Base::$filter_mode = ORM_Base::FILTER_FRONTEND;
     ORM_Base::$site_id = $request->site_id;
     ORM_Base::$site_id_master = $request->site_id_master;
     if ($request->route() !== NULL) {
         return $request;
     }
     $request_uri = $request->uri();
     $request->page = $page = Page_Route::page($request->uri());
     if ($page !== NULL) {
         $routes = array();
         if ($page['type'] == 'module' and !Helper_Module::is_stanalone($page['data'])) {
             $routes_config = Kohana::$config->load('routes/' . $page['data'])->as_array();
             $request->set_module_routes($routes_config, $page['uri_full'], $page['id'], $cache);
         } elseif (Helper_Module::is_stanalone($page['data'])) {
             /*
              * For controllers which no need admin side (only public contoller) 
              * and have one action (by default is 'index')
              * Can have route file or default controller (controller name equal 'data' field value) 
              */
             $routes_config = Kohana::$config->load('routes/' . $page['data'])->as_array();
             if (empty($routes_config)) {
                 $name = $page['id'] . '<->standalone_page';
                 $uri_callback = $page['uri_full'];
                 $defaults = array('directory' => 'standalone', 'controller' => $page['data']);
                 $route = new Route($uri_callback);
                 $route->defaults($defaults);
                 $routes[$name] = $route;
                 Route::set($name, $uri_callback)->defaults($defaults);
                 $processed_uri = Request::process_uri($request_uri, $routes);
                 if ($processed_uri !== NULL) {
                     $request->set_dinamic_route($processed_uri, $cache);
                 }
             } else {
                 $request->set_module_routes($routes_config, $page['uri_full'], $page['id'], $cache);
             }
         } else {
             /*
              * For simple static pages
              */
             $name = $page['id'] . '<->std_page';
             $uri_callback = $page['uri_full'];
             $defaults = array('controller' => 'page', 'action' => $page['type']);
             $route = new Route($uri_callback);
             $route->defaults($defaults);
             $routes[$name] = $route;
             Route::set($name, $uri_callback)->defaults($defaults);
             $processed_uri = Request::process_uri($request_uri, $routes);
             if ($processed_uri !== NULL) {
                 $request->set_dinamic_route($processed_uri, $cache);
             }
         }
     } else {
         Kohana::$log->add(Log::ERROR, 'Page for :uri not found. [:file][:line] ', array(':file' => Debug::path(__FILE__), ':line' => __LINE__, ':uri' => $request->uri()));
         throw new HTTP_Exception_404();
     }
     return $request;
 }