protected function setUp() { // Save config $this->kohana_config['core.url_suffix'] = Kohana_Config::instance()->get('core.url_suffix'); // Save Server API $this->kohana_server_api = Kohana::$server_api; // Save Router members $this->router_vars = array('complete_uri' => Router::$complete_uri, 'controller' => Router::$controller, 'current_uri' => Router::$current_uri, 'query_string' => Router::$query_string, 'rsegments' => Router::$rsegments, 'routed_uri' => Router::$routed_uri, 'segments' => Router::$segments, 'url_suffix' => Router::$url_suffix); // Reset Router members Router::$complete_uri = ''; Router::$controller = NULL; Router::$current_uri = ''; Router::$query_string = ''; Router::$rsegments = NULL; Router::$routed_uri = ''; Router::$segments = NULL; Router::$url_suffix = ''; }
/** * Router setup routine. Automatically called during Kohana setup process. * * @return void */ public static function setup() { if (!empty($_SERVER['QUERY_STRING'])) { // Set the query string to the current query string Router::$query_string = '?' . trim($_SERVER['QUERY_STRING'], '&/'); } if (Router::$routes === NULL) { // Load routes Router::$routes = Kohana::config('routes'); } // Default route status $default_route = FALSE; if (Router::$current_uri === '') { // Make sure the default route is set if (!isset(Router::$routes['_default'])) { throw new Kohana_Exception('core.no_default_route'); } // Use the default route when no segments exist Router::$current_uri = Router::$routes['_default']; // Default route is in use $default_route = TRUE; } // Make sure the URL is not tainted with HTML characters Router::$current_uri = html::specialchars(Router::$current_uri, FALSE); // Remove all dot-paths from the URI, they are not valid Router::$current_uri = preg_replace('#\\.[\\s./]*/#', '', Router::$current_uri); // At this point segments, rsegments, and current URI are all the same Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/'); // Set the complete URI Router::$complete_uri = Router::$current_uri . Router::$query_string; // Explode the segments by slashes Router::$segments = ($default_route === TRUE or Router::$segments === '') ? array() : explode('/', Router::$segments); if ($default_route === FALSE and count(Router::$routes) > 1) { // Custom routing Router::$rsegments = Router::routed_uri(Router::$current_uri); } // The routed URI is now complete Router::$routed_uri = Router::$rsegments; // Routed segments will never be empty Router::$rsegments = explode('/', Router::$rsegments); // Prepare to find the controller $controller_path = ''; $method_segment = NULL; // Paths to search $paths = Kohana::include_paths(); foreach (Router::$rsegments as $key => $segment) { // Add the segment to the search path $controller_path .= $segment; $found = FALSE; foreach ($paths as $dir) { // Search within controllers only $dir .= 'controllers/'; if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . EXT)) { // Valid path $found = TRUE; // The controller must be a file that exists with the search path if ($c = str_replace('\\', '/', realpath($dir . $controller_path . EXT)) and is_file($c) and strpos($c, $dir) === 0) { // Set controller name Router::$controller = $segment; // Change controller path Router::$controller_path = $c; // Set the method segment $method_segment = $key + 1; // Stop searching break; } } } if ($found === FALSE) { // Maximum depth has been reached, stop searching break; } // Add another slash $controller_path .= '/'; } if ($method_segment !== NULL and isset(Router::$rsegments[$method_segment])) { // Set method Router::$method = Router::$rsegments[$method_segment]; if (isset(Router::$rsegments[$method_segment + 1])) { // Set arguments Router::$arguments = array_slice(Router::$rsegments, $method_segment + 1); } } // Last chance to set routing before a 404 is triggered Event::run('system.post_routing'); if (Router::$controller === NULL) { // No controller was found, so no page can be rendered Event::run('system.404'); } }
private static function route($priority) { $uri = implode('/', segments()); foreach (self::$slugs as $key => $routes) { $key = str_replace('%s', '([a-z0-9\\-]+)', str_replace('%d', '([0-9]+)', str_replace('/', '\\/', $key))); if (preg_match('/^' . $key . '(\\/|$)/is', $uri, $matches)) { array_pop($matches); array_shift($matches); foreach ($routes as &$route) { if ($route->get_priority() == $priority) { $arguments = preg_replace('/^' . $key . '(\\/|$)/is', '', $uri); $arguments = explode("?", $arguments); $arguments = array_filter(explode("/", array_shift($arguments))); $arguments = array_merge($matches, $arguments); // Check if the class that will handle the content actually contains the requested function. if (!method_exists($route->get_classname(), $route->get_function())) { continue; } // Check if we're not calling said function with too few parameters. $reflector = new ReflectionClass($route->get_classname()); if (count($arguments) < $reflector->getMethod($route->get_function())->getNumberOfRequiredParameters()) { continue; } // Check if this function might want variable number of parameters. $collapse_parameters = false; $parameters = $reflector->getMethod($route->get_function())->getParameters(); if (count($parameters) && end($parameters)->name == 'parameters') { $collapse_parameters = true; } if (count($arguments) > count($parameters) && $collapse_parameters && $priority == ROUTE_DEFAULT) { $route->set_priority(ROUTE_LATE); continue; } // Check if we're not calling said function with too many parameters. if (count($arguments) > count($parameters) && !$collapse_parameters) { continue; } // Check if we're not calling a static function. if ($reflector->getMethod($route->get_function())->isStatic()) { continue; } // Save old segments should we need it again later self::$urlsegments = self::$segments; // Set the segments to those that matched our content self::$segments = array(); self::$segments[0] = strtolower($route->get_contentname()); self::$segments[1] = strtolower($route->get_function()); self::$segments = array_merge(self::$segments, $arguments); // Set the current route self::$current_route = $route; // Check database if needed (only do this when there's no admin panel) if (!Config::admin_enabled()) { $site = current_site(); if (self::is_fw4() && !$site->live) { FW4_Structure::check_structure(); } } // Fire the controller View_Loader::get_instance()->set_path(CONTENTPATH . self::$content_prefix . self::$segments[0]); $page = self::$content_pages[strtolower($route->get_classname())]; if ($collapse_parameters) { $non_optional = array_splice($arguments, 0, count($parameters) - 1); $arguments = array_merge($non_optional, array(array_diff($arguments, array('index')))); } try { $result = call_user_func_array(array($page, $route->get_function()), $arguments); } catch (RowNotFoundException $e) { $result = false; } // If the controller returns false, reset the segments and continue matching if ($result === false) { self::$segments = self::$urlsegments; continue; } return true; } } } } return false; }
public function splitUrl() { self::$segments = explode('/', $this->uri); }
<?php /* APPLICATION ROUTES */ // SET SEGMENTS LIMIT Router::$segments = 10; //APPLICATION ROUTES Route::get('(.*)', array('as' => 'master', 'before' => 'init', function ($url) { return CmsRender::page(); })); //APPLICATION CONTROLLERS Route::controller(Controller::detect()); //APPLICATION FILTER FRONTEND Route::filter('init', function () { //SAVE SESSION CREDENTIAL if (Auth::check() and is_numeric(AUTHORID)) { Session::put('USERID', Auth::user()->id); Session::put('USERNAME', Auth::user()->username); Session::put('EMAIL', Auth::user()->email); Session::put('ROLEID', Auth::user()->role_id); Session::put('ROLE', Auth::user()->role_level); Session::put('USERLANG', Auth::user()->lang); } else { Session::put('USERID', 0); Session::put('USERNAME', ''); Session::put('EMAIL', ''); Session::put('ROLEID', 0); Session::put('ROLE', 0); Session::put('USERLANG', Config::get('application.language')); } //LOAD SEGMENTS $segment = CmsUtility::url_segments();