mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-08-21 15:51:28 +02:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
09d9d946c5 | ||
|
b749633151 | ||
|
b29440e117 | ||
|
dbe416b9b9 | ||
|
fc6d743e0a | ||
|
fb41766c3d | ||
|
1303c3a0af | ||
|
4f8836e179 | ||
|
c3c166bd76 | ||
|
cace1a6f97 |
@@ -4,4 +4,4 @@ php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
|
||||
script: phpunit ./
|
||||
script: phpunit --coverage-text ./
|
||||
|
@@ -22,11 +22,28 @@ class AltoRouter {
|
||||
* @param array $matchTypes
|
||||
*/
|
||||
public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) {
|
||||
$this->addRoutes($routes);
|
||||
$this->setBasePath($basePath);
|
||||
$this->addMatchTypes($matchTypes);
|
||||
}
|
||||
|
||||
foreach( $routes as $route ) {
|
||||
call_user_func_array(array($this,'map'),$route);
|
||||
/**
|
||||
* Add multiple routes at once from array in the following format:
|
||||
*
|
||||
* $routes = array(
|
||||
* array($method, $route, $target, $name)
|
||||
* );
|
||||
*
|
||||
* @param array $routes
|
||||
* @return void
|
||||
* @author Koen Punt
|
||||
*/
|
||||
public function addRoutes($routes){
|
||||
if(!is_array($routes) && !$routes instanceof Traversable) {
|
||||
throw new \Exception('Routes should be an array or an instance of Traversable');
|
||||
}
|
||||
foreach($routes as $route) {
|
||||
call_user_func_array(array($this, 'map'), $route);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,7 +71,6 @@ class AltoRouter {
|
||||
* @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id]
|
||||
* @param mixed $target The target where this route should point to. Can be anything.
|
||||
* @param string $name Optional name of this route. Supply if you want to reverse route this url in your application.
|
||||
*
|
||||
*/
|
||||
public function map($method, $route, $target, $name = null) {
|
||||
|
||||
@@ -170,7 +186,7 @@ class AltoRouter {
|
||||
if ($_route === '*') {
|
||||
$match = true;
|
||||
} elseif (isset($_route[0]) && $_route[0] === '@') {
|
||||
$match = preg_match('`' . substr($_route, 1) . '`', $requestUrl, $params);
|
||||
$match = preg_match('`' . substr($_route, 1) . '`u', $requestUrl, $params);
|
||||
} else {
|
||||
$route = null;
|
||||
$regex = false;
|
||||
@@ -226,7 +242,7 @@ class AltoRouter {
|
||||
if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
|
||||
|
||||
$matchTypes = $this->matchTypes;
|
||||
foreach ($matches as $match) {
|
||||
foreach($matches as $match) {
|
||||
list($block, $pre, $type, $param, $optional) = $match;
|
||||
|
||||
if (isset($matchTypes[$type])) {
|
||||
@@ -249,6 +265,6 @@ class AltoRouter {
|
||||
}
|
||||
|
||||
}
|
||||
return "`^$route$`";
|
||||
return "`^$route$`u";
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,33 @@ class AltoRouterDebug extends AltoRouter{
|
||||
|
||||
}
|
||||
|
||||
class SimpleTraversable implements Iterator{
|
||||
|
||||
protected $_position = 0;
|
||||
|
||||
protected $_data = array(
|
||||
array('GET', '/foo', 'foo_action', null),
|
||||
array('POST', '/bar', 'bar_action', 'second_route')
|
||||
);
|
||||
|
||||
public function current(){
|
||||
return $this->_data[$this->_position];
|
||||
}
|
||||
public function key(){
|
||||
return $this->_position;
|
||||
}
|
||||
public function next(){
|
||||
++$this->_position;
|
||||
}
|
||||
public function rewind(){
|
||||
$this->_position = 0;
|
||||
}
|
||||
public function valid(){
|
||||
return isset($this->_data[$this->_position]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2013-07-14 at 17:47:46.
|
||||
*/
|
||||
@@ -45,6 +72,55 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutes()
|
||||
{
|
||||
$method = 'POST';
|
||||
$route = '/[:controller]/[:action]';
|
||||
$target = function(){};
|
||||
|
||||
$this->router->addRoutes(array(
|
||||
array($method, $route, $target),
|
||||
array($method, $route, $target, 'second_route')
|
||||
));
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals(array($method, $route, $target, null), $routes[0]);
|
||||
$this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
*/
|
||||
public function testAddRoutesAcceptsTraverable()
|
||||
{
|
||||
$traversable = new SimpleTraversable();
|
||||
$this->router->addRoutes($traversable);
|
||||
|
||||
$traversable->rewind();
|
||||
|
||||
$first = $traversable->current();
|
||||
$traversable->next();
|
||||
$second = $traversable->current();
|
||||
|
||||
$routes = $this->router->getRoutes();
|
||||
|
||||
$this->assertEquals($first, $routes[0]);
|
||||
$this->assertEquals($second, $routes[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addRoutes
|
||||
* @expectedException Exception
|
||||
*/
|
||||
public function testAddRoutesThrowsExceptionOnInvalidArgument()
|
||||
{
|
||||
$this->router->addRoutes(new stdClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::setBasePath
|
||||
*/
|
||||
@@ -266,6 +342,36 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithUnicodeRegex()
|
||||
{
|
||||
$pattern = '/(?<path>[^';
|
||||
// Arabic characters
|
||||
$pattern .= '\x{0600}-\x{06FF}';
|
||||
$pattern .= '\x{FB50}-\x{FDFD}';
|
||||
$pattern .= '\x{FE70}-\x{FEFF}';
|
||||
$pattern .= '\x{0750}-\x{077F}';
|
||||
// Alphanumeric, /, _, - and space characters
|
||||
$pattern .= 'a-zA-Z0-9\/_-\s';
|
||||
// 'ZERO WIDTH NON-JOINER'
|
||||
$pattern .= '\x{200C}';
|
||||
$pattern .= ']+)';
|
||||
|
||||
$this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'unicode_action',
|
||||
'name' => 'unicode_route',
|
||||
'params' => array(
|
||||
'path' => '大家好'
|
||||
)
|
||||
), $this->router->match('/大家好', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/﷽', 'GET'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers AltoRouter::addMatchTypes
|
||||
*/
|
||||
public function testMatchWithCustomNamedRegex()
|
||||
{
|
||||
$this->router->addMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?'));
|
||||
@@ -290,4 +396,28 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($this->router->match('/some-other-thing', 'GET'));
|
||||
|
||||
}
|
||||
|
||||
public function testMatchWithCustomNamedUnicodeRegex()
|
||||
{
|
||||
$pattern = '[^';
|
||||
// Arabic characters
|
||||
$pattern .= '\x{0600}-\x{06FF}';
|
||||
$pattern .= '\x{FB50}-\x{FDFD}';
|
||||
$pattern .= '\x{FE70}-\x{FEFF}';
|
||||
$pattern .= '\x{0750}-\x{077F}';
|
||||
$pattern .= ']+';
|
||||
|
||||
$this->router->addMatchTypes(array('nonArabic' => $pattern));
|
||||
$this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route');
|
||||
|
||||
$this->assertEquals(array(
|
||||
'target' => 'non_arabic_action',
|
||||
'name' => 'non_arabic_route',
|
||||
'params' => array(
|
||||
'string' => 'some-path'
|
||||
)
|
||||
), $this->router->match('/bar/some-path', 'GET'));
|
||||
|
||||
$this->assertFalse($this->router->match('/﷽', 'GET'));
|
||||
}
|
||||
}
|
||||
|
@@ -12,7 +12,7 @@ AltoRouter is a small but powerful routing class for PHP 5.3+, heavily inspired
|
||||
2. Install AltoRouter using Composer or manually
|
||||
2. Setup URL rewriting so that all requests are handled by **index.php**
|
||||
3. Create an instance of AltoRouter, map your routes and match a request.
|
||||
4. Have a look at the example `index.php` file for a better understanding on how to use AltoRouter(index.php).
|
||||
4. Have a look at the basic example in the `examples` directory for a better understanding on how to use AltoRouter.
|
||||
|
||||
## Routing
|
||||
```php
|
||||
|
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
require 'AltoRouter.php';
|
||||
require '../../AltoRouter.php';
|
||||
|
||||
$router = new AltoRouter();
|
||||
$router->setBasePath('/AltoRouter');
|
||||
$router->setBasePath('/AltoRouter/examples/basic');
|
||||
$router->map('GET|POST','/', 'home#index', 'home');
|
||||
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||
@@ -12,7 +12,7 @@ $router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doA
|
||||
// match current request
|
||||
$match = $router->match();
|
||||
?>
|
||||
<h1>AltoRouter</h3>
|
||||
<h1>AltoRouter</h1>
|
||||
|
||||
<h3>Current request: </h3>
|
||||
<pre>
|
Reference in New Issue
Block a user