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

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

@ -34,11 +34,13 @@ class RouterTest extends PHPUnit_Framework_TestCase
// Checks if a route was matched // Checks if a route was matched
function check($str = 'OK'){ function check($str = 'OK'){
$route = $this->router->route($this->request); $route = $this->router->route($this->request);
$params = array_values($route->params); $params = array_values($route->params);
array_push($params, $route);
$this->assertTrue(is_callable($route->callback)); $this->assertTrue(is_callable($route->callback));
call_user_func_array($route->callback, $route->params); call_user_func_array($route->callback, $params);
$this->expectOutputString($str); $this->expectOutputString($str);
} }
@ -146,4 +148,14 @@ class RouterTest extends PHPUnit_Framework_TestCase
$this->check(); $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