/** * Adds a route to the array of routes with the option of placing it before a specific * other named route in the array. * @static * @param $name * @param $uri * @param $regex * @param $before * @return void */ public static function add($name, $uri, array $regex = NULL, $before = null) { if (empty($before)) { return parent::set($name, $uri, $regex); } $keys = array_keys(Route::$_routes); //Jx_Debug::dump($keys,'route keys before replacement'); $index = array_search($before, $keys); //Jx_Debug::dump($index,'key index'); if (false !== $index) { //Jx_Debug::dump('in right spot'); $values = array_values(Route::$_routes); //Jx_Debug::dump($values,'values before replacement'); array_splice($keys, $index, 0, $name); //Jx_Debug::dump($keys,'route keys after replacement'); array_splice($values, $index, 0, array(new Route($uri, $regex))); //Jx_Debug::dump($values,'values after replacement'); Route::$_routes = array_combine($keys, $values); //Jx_Debug::dump(Route::$_routes,'routes after adding test'); return Route::$_routes[$name]; } else { //Jx_Debug::dump('in bad spot'); return parent::set($name, $uri, $regex); } }
/** * Generate the url for the specified page. * * @param int $page * @return String */ public function url($page = 1) { // Clean the page number $page = max(1, (int) $page); // No page number in URLs to first page if ($page === 1 and !$this->config['first_page_in_url']) { $page = NULL; } $params = array_merge($this->route_params, array($this->config['param'] => $page)); $suffix = ''; if ($this->query != FALSE) { if (is_string($this->query)) { $suffix = $this->query != FALSE ? '?' . $this->query . '=' . $this->request->query($this->query) : ''; } elseif (is_array($this->query)) { $suffix = '?'; $first = TRUE; foreach ($this->query as $q) { if ($first) { $first = FALSE; $suffix .= $q . '=' . $this->request->query($q); } else { $suffix .= '&' . $q . '=' . $this->request->query($q); } } } } return URL::site($this->route->uri($params)) . $suffix; }
public function matches($uri) { $matches = parent::matches($uri); if (isset($matches['action'])) { $matches['action'] = str_replace('-', '_', $matches['action']); } return $matches; }
/** * Rails-style defaults: * Route::set('name', 'articles')->to('Articles#index'); * Route::root()->to('Article#index'); */ public function to($defaults) { if (is_string($defaults) && preg_match('~(?:(?<directory>\\w+)/)?(?<controller>\\w+)(?:#(?<action>\\w+))?~', $defaults, $matches)) { $defaults = ['controller' => $matches['controller'], 'action' => $matches['action'] ? $matches['action'] : 'index']; if ($matches['directory']) { $defaults['directory'] = $matches['directory']; } } return parent::defaults((array) $defaults); }
/** * Stores a named route and returns it. The "action" will always be set to * "index" if it is not defined. * * Route::set('default', '(<controller>(/<action>(/<id>)))') * ->defaults(array( * 'controller' => 'welcome', * )); * * @param $name string route name * @param $uri string URI pattern * @param $regex array regex patterns for route keys * @param $options array Optional method and resource for the route * @return Route */ public static function set($name, $uri = NULL, $regex = NULL, array $options = array()) { $route = parent::set($name, $uri, $regex); $route->_resource_name = Arr::get($options, 'resource'); $route->method(Arr::get($options, 'method')); if ($route->_method) { $route->filter(array('Resource_Route', 'is_method_allowed')); } return $route; }
/** * Generates a translated URI for the current route based on the parameters given * * @param array $params parameters for URI * @param string $lang target language, defaults to I18n::$lang * @return string URI in target language * @uses Lang::$i18n_routes * @uses Route::uri * @uses I18n::$lang * @uses Route::map */ public function uri(array $params = NULL, $lang = NULL) { // Forced language $forced_language = $lang !== NULL; if ($lang === NULL) { // Set target language to current language $lang = Lang::shortcode(I18n::$lang); } if (!Lang::$i18n_routes) { // i18n routes are off, build URI $uri = parent::uri($params); if (Lang::$default_prepended or Request::$lang !== Lang::$default and $lang !== Lang::$default or $forced_language and $lang !== Lang::$default) { // Prepend the target language to the URI if needed $uri = $lang . '/' . ltrim($uri, '/'); } // Return URI with or without language return $uri; } // Make sure text values are in correct language $this->translate_route($lang, FALSE); if ($params !== NULL) { // Translation required foreach ($params as $label => &$param) { if (isset($this->_translate['<' . $label . '>']) or isset($this->_translate[$param])) { // Translate param $param = Route::map($param, $lang); } } } // Build URI $uri = parent::uri($params); if (Lang::$default_prepended or Request::$lang !== Lang::$default and $lang !== Lang::$default or $forced_language and $lang !== Lang::$default) { // Prepend the target language to the URI if needed $uri = $lang . '/' . ltrim($uri, '/'); } return $uri; }