Added ability to perform case sensitive matching of URLs.

pull/226/head v1.2.15
Mike Cao 9 years ago
parent 1feeb95ff2
commit 7fd3594ac7

@ -824,6 +824,7 @@ Flight::set('flight.log_errors', true);
The following is a list of all the available configuration settings:
flight.base_url - Override the base url of the request. (default: null)
flight.case_sensitive - Case sensitive matching for URLs. (default: false)
flight.handle_errors - Allow Flight to handle all errors internally. (default: true)
flight.log_errors - Log errors to the web server's error log file. (default: false)
flight.views.path - Directory containing view template files. (default: ./views)

@ -1 +1 @@
1.2.15
1.2.16

@ -103,6 +103,7 @@ class Engine {
// Default configuration settings
$this->set('flight.base_url', null);
$this->set('flight.case_sensitive', false);
$this->set('flight.handle_errors', true);
$this->set('flight.log_errors', false);
$this->set('flight.views.path', './views');
@ -302,6 +303,9 @@ class Engine {
$self->stop();
});
// Set case-sensitivity
$router->case_sensitive = $this->get('flight.case_sensitive');
// Route the request
while ($route = $router->route($request)) {
$params = array_values($route->params);

@ -68,9 +68,10 @@ class Route {
* Checks if a URL matches the route pattern. Also parses named parameters in the URL.
*
* @param string $url Requested URL
* @param boolean $case_sensitive Case sensitive matching
* @return boolean Match status
*/
public function matchUrl($url) {
public function matchUrl($url, $case_sensitive = false) {
// Wildcard or exact match
if ($this->pattern === '*' || $this->pattern === $url) {
if ($this->pass) {
@ -121,7 +122,7 @@ class Route {
}
// Attempt to match route and named parameters
if (preg_match('#^'.$regex.'(?:\?.*)?$#i', $url, $matches)) {
if (preg_match('#^'.$regex.'(?:\?.*)?$#'.(($case_sensitive) ? '' : 'i'), $url, $matches)) {
foreach ($ids as $k => $v) {
$this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null;
}

@ -22,12 +22,19 @@ class Router {
protected $routes = array();
/**
* Pointer to current route
* Pointer to current route.
*
* @var int
*/
protected $index = 0;
/**
* Case sensitive matching.
*
* @var boolean
*/
public $case_sensitive = false;
/**
* Gets mapped routes.
*
@ -72,7 +79,7 @@ class Router {
*/
public function route(Request $request) {
while ($route = $this->current()) {
if ($route !== false && $route->matchMethod($request->method) && $route->matchUrl($request->url)) {
if ($route !== false && $route->matchMethod($request->method) && $route->matchUrl($request->url, $this->case_sensitive)) {
return $route;
}
$this->next();

@ -24,6 +24,7 @@ class RouterTest extends PHPUnit_Framework_TestCase
function setUp(){
$this->router = new \flight\net\Router();
$this->request = new \flight\net\Request();
$this->dispatcher = new \flight\core\Dispatcher();
}
// Simple output
@ -32,7 +33,8 @@ class RouterTest extends PHPUnit_Framework_TestCase
}
// Checks if a route was matched with a given output
function check($str = ''){
function check($str = '') {
/*
$route = $this->router->route($this->request);
$params = array_values($route->params);
@ -40,10 +42,37 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->assertTrue(is_callable($route->callback));
call_user_func_array($route->callback, $params);
*/
$this->routeRequest();
$this->expectOutputString($str);
}
function routeRequest() {
$dispatched = false;
while ($route = $this->router->route($this->request)) {
$params = array_values($route->params);
$continue = $this->dispatcher->execute(
$route->callback,
$params
);
$dispatched = true;
if (!$continue) break;
$this->router->next();
$dispatched = false;
}
if (!$dispatched) {
echo '404';
}
}
// Default route
function testDefaultRoute(){
$this->router->map('/', array($this, 'ok'));
@ -201,4 +230,21 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->check('456/def/xyz');
}
// Test not found
function testNotFound() {
$this->router->map('/does_exist', array($this, 'ok'));
$this->request->url = '/does_not_exist';
$this->check('404');
}
// Test case sensitivity
function testCaseSensitivity() {
$this->router->map('/hello', array($this, 'ok'));
$this->request->url = '/HELLO';
$this->router->case_sensitive = true;
$this->check('404');
}
}

Loading…
Cancel
Save