Route
› Kohana_Route
Class Contents
Constants
Properties
Class declared in SYSPATH/classes/route.php on line 3.
Constants
- REGEX_KEY
string(18) "<([a-zA-Z0-9_]++)>"- REGEX_SEGMENT
string(12) "[^/.,;?\n]++"- REGEX_ESCAPE
string(17) "[.\+*?[^\]${}=!|]"
Properties
- public static
$default_action string(5) "index"
Methods
public __construct( )
› Kohana_Route
Source Code
public function __construct($uri = NULL, array $regex = NULL)
{
if ($uri === NULL)
{
// Assume the route is from cache
return;
}
if ( ! empty($regex))
{
$this->_regex = $regex;
}
// Store the URI that this route will match
$this->_uri = $uri;
// Store the compiled regex locally
$this->_route_regex = $this->_compile();
}
protected _compile( )
› Kohana_Route
Source Code
protected function _compile()
{
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$regex = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $this->_uri);
if (strpos($regex, '(') !== FALSE)
{
// Make optional parts of the URI non-capturing and optional
$regex = str_replace(array('(', ')'), array('(?:', ')?'), $regex);
}
// Insert default regex for keys
$regex = str_replace(array('<', '>'), array('(?P<', '>'.Route::REGEX_SEGMENT.')'), $regex);
if ( ! empty($this->_regex))
{
$search = $replace = array();
foreach ($this->_regex as $key => $value)
{
$search[] = "<$key>".Route::REGEX_SEGMENT;
$replace[] = "<$key>$value";
}
// Replace the default regex with the user-specified regex
$regex = str_replace($search, $replace, $regex);
}
return '#^'.$regex.'$#uD';
}
public static all( )
› Kohana_Route
Source Code
public static function all()
{
return Route::$_routes;
}
public static cache( )
› Kohana_Route
Source Code
public static function cache($save = FALSE)
{
if ($save === TRUE)
{
// Cache all defined routes
Kohana::cache('Route::cache()', Route::$_routes);
}
else
{
if ($routes = Kohana::cache('Route::cache()'))
{
Route::$_routes = $routes;
// Routes were cached
return TRUE;
}
else
{
// Routes were not cached
return FALSE;
}
}
}
public defaults( )
› Kohana_Route
Source Code
public function defaults(array $defaults = NULL)
{
$this->_defaults = $defaults;
return $this;
}
public static get( )
› Kohana_Route
Source Code
public static function get($name)
{
if ( ! isset(Route::$_routes[$name]))
{
throw new Kohana_Exception('The requested route does not exist: :route',
array(':route' => $name));
}
return Route::$_routes[$name];
}
public matches( )
› Kohana_Route
Source Code
public function matches($uri)
{
if ( ! preg_match($this->_route_regex, $uri, $matches))
return FALSE;
$params = array();
foreach ($matches as $key => $value)
{
if (is_int($key))
{
// Skip all unnamed keys
continue;
}
// Set the value for all matched keys
$params[$key] = $value;
}
foreach ($this->_defaults as $key => $value)
{
if ( ! isset($params[$key]) OR $params[$key] === '')
{
// Set default values for any key that was not matched
$params[$key] = $value;
}
}
return $params;
}
public static name( )
› Kohana_Route
Source Code
public static function name(Route $route)
{
return array_search($route, Route::$_routes);
}
public static set( )
› Kohana_Route
Source Code
public static function set($name, $uri, array $regex = NULL)
{
return Route::$_routes[$name] = new Route($uri, $regex);
}
public uri( )
› Kohana_Route
Source Code
public function uri(array $params = NULL)
{
if ($params === NULL)
{
// Use the default parameters
$params = $this->_defaults;
}
else
{
// Add the default parameters
$params += $this->_defaults;
}
// Start with the routed URI
$uri = $this->_uri;
if (strpos($uri, '<') === FALSE AND strpos($uri, '(') === FALSE)
{
// This is a static route, no need to replace anything
return $uri;
}
while (preg_match('#\([^()]++\)#', $uri, $match))
{
// Search for the matched value
$search = $match[0];
// Remove the parenthesis from the match as the replace
$replace = substr($match[0], 1, -1);
while (preg_match('#'.Route::REGEX_KEY.'#', $replace, $match))
{
list($key, $param) = $match;
if (isset($params[$param]))
{
// Replace the key with the parameter value
$replace = str_replace($key, $params[$param], $replace);
}
else
{
// This group has missing parameters
$replace = '';
break;
}
}
// Replace the group in the URI
$uri = str_replace($search, $replace, $uri);
}
while (preg_match('#'.Route::REGEX_KEY.'#', $uri, $match))
{
list($key, $param) = $match;
if ( ! isset($params[$param]))
{
// Ungrouped parameters are required
throw new Kohana_Exception('Required route parameter not passed: :param',
array(':param' => $param));
}
$uri = str_replace($key, $params[$param], $uri);
}
// Trim all extra slashes from the URI
$uri = preg_replace('#//+#', '/', rtrim($uri, '/'));
return $uri;
}
public static url( )
› Kohana_Route
Source Code
public static function url($name, array $params = NULL, $protocol = NULL)
{
// Create a URI with the route and convert it to a URL
return URL::site(Route::get($name)->uri($params), $protocol);
}