Skip to content

Instantly share code, notes, and snippets.

@biakaveron
Created January 29, 2011 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biakaveron/802262 to your computer and use it in GitHub Desktop.
Save biakaveron/802262 to your computer and use it in GitHub Desktop.
routes with prefixes and suffixes
<?php defined('SYSPATH') OR die('No direct access allowed.');
class Route extends Kohana_Route {
protected static $_prefix = array();
protected static $_suffix = array();
protected function _patch()
{
if ( ! empty(self::$_prefix))
{
$this->_uri = self::$_prefix['segments'].$this->_uri;
$this->_regex = $this->_regex + self::$_prefix['regex'];
$this->defaults($this->_defaults + self::$_prefix['defaults']);
}
if ( ! empty(self::$_suffix))
{
$this->_uri = $this->_uri.self::$_suffix['segments'];
$this->_regex = $this->_regex + self::$_suffix['regex'];
$this->defaults($this->_defaults + self::$_suffix['defaults']);
}
if ( ! empty(self::$_prefix) OR ! empty(self::$_suffix))
{
$this->_route_regex = Route::compile($this->_uri, $this->_regex);
}
}
public static function prefix($segment, array $regex, array $default = array())
{
if (empty(self::$_prefix))
{
self::$_prefix = array(
'segments' => '',
'regex' => array(),
'defaults' => array(),
);
}
self::$_prefix['segments'] = self::$_prefix['segments'].$segment;
self::$_prefix['regex'] = self::$_prefix['regex'] + $regex;
self::$_prefix['defaults'] = self::$_prefix['defaults'] + $default;
}
public static function suffix($segment, array $regex, array $default = array())
{
if (empty(self::$_suffix))
{
self::$_suffix = array(
'segments' => '',
'regex' => array(),
'defaults' => array(),
);
}
self::$_suffix['segments'] = $segment.self::$_suffix['segments'];
self::$_suffix['regex'] = self::$_suffix['regex'] + $regex;
self::$_suffix['defaults'] = self::$_suffix['defaults'] + $default;
}
public function __construct($uri = NULL, $regex = NULL)
{
if ($uri === NULL)
{
// Assume the route is from cache
return;
}
parent::__construct($uri, $regex);
if ($this->_callback)
{
// Skip callback routes
return;
}
// Apply prefixes & suffixes
$this->_patch();
}
}
/**
in bootstrap.php:
Route::prefix('(<lang>/)', array('lang' => '[a-z]{2}'));
Route::suffix('(.<format>)', array('format' => '[a-z]+'));
So, ru/welcome/index.html will work for default route.
**/
@biakaveron
Copy link
Author

not work for routes with callbacks or lambdas :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment