Added ability to get splat from URL pattern.

pull/87/head
Mike Cao 11 years ago
parent 43287023d9
commit 7817255a61

@ -1 +1 @@
1.1.1
1.1.2

@ -274,10 +274,13 @@ class Engine {
public function _start() {
$dispatched = false;
$self = $this;
$request = $this->request();
$response = $this->response();
$router = $this->router();
// Flush any existing output
if (ob_get_length() > 0) {
$this->response()->write(ob_get_clean());
$response->write(ob_get_clean());
}
// Enable output buffering
@ -287,8 +290,8 @@ class Engine {
$this->handleErrors($this->get('flight.handle_errors'));
// Disable caching for AJAX requests
if ($this->request()->ajax) {
$this->response()->cache(false);
if ($request->ajax) {
$response->cache(false);
}
// Allow post-filters to run
@ -297,8 +300,9 @@ class Engine {
});
// Route the request
while ($route = $this->router()->route($this->request())) {
while ($route = $router->route($request)) {
$params = array_values($route->params);
array_push($params, $route);
$continue = $this->dispatcher->execute(
$route->callback,
@ -309,7 +313,7 @@ class Engine {
if (!$continue) break;
$this->router()->next();
$router->next();
}
if (!$dispatched) {

@ -39,6 +39,11 @@ class Route {
*/
public $regex;
/**
* @var string URL splat content
*/
public $splat;
/**
* Constructor.
*
@ -65,6 +70,8 @@ class Route {
$ids = array();
$char = substr($this->pattern, -1);
$this->splat = substr($url, strpos($this->pattern, '*'));
$this->pattern = str_replace(array(')','*'), array(')?','.*?'), $this->pattern);
// Build the regex for matching

@ -34,11 +34,13 @@ class RouterTest extends PHPUnit_Framework_TestCase
// Checks if a route was matched
function check($str = 'OK'){
$route = $this->router->route($this->request);
$params = array_values($route->params);
array_push($params, $route);
$this->assertTrue(is_callable($route->callback));
call_user_func_array($route->callback, $route->params);
call_user_func_array($route->callback, $params);
$this->expectOutputString($str);
}
@ -146,4 +148,14 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->check();
}
// Test splat
function testSplatWildcard(){
$this->router->map('/account/*', function($route){
echo $route->splat;
});
$this->request->url = '/account/123/abc/xyz';
$this->check('123/abc/xyz');
}
}

Loading…
Cancel
Save