removed provides functionality. need to explore doing this in a way that is more friendly to http, such as using the accept header.

This commit is contained in:
Taylor Otwell 2011-10-29 21:02:01 -05:00
parent a2aebdb03f
commit 866f5d8fc3
4 changed files with 33 additions and 82 deletions

View File

@ -91,13 +91,13 @@
*/ */
Routing\Filter::register(require APP_PATH.'filters'.EXT); Routing\Filter::register(require APP_PATH.'filters'.EXT);
list($uri, $method, $format) = array(Request::uri()->get(), Request::method(), Request::format()); list($uri, $method) = array(Request::uri()->get(), Request::method());
$route = IoC::container()->core('routing.router')->route($method, $uri, $format); Request::$route = IoC::container()->core('routing.router')->route($method, $uri);
if ( ! is_null($route)) if ( ! is_null(Request::$route))
{ {
$response = $route->call(); $response = Request::$route->call();
} }
else else
{ {

View File

@ -33,21 +33,6 @@ public static function uri()
return (is_null(static::$uri)) ? static::$uri = new URI($_SERVER) : static::$uri; return (is_null(static::$uri)) ? static::$uri = new URI($_SERVER) : static::$uri;
} }
/**
* Get the request format.
*
* The format is determined by taking the "extension" of the URI.
*
* @param string $uri
* @return string
*/
public static function format($uri = null)
{
if (is_null($uri)) $uri = static::uri()->get();
return (($extension = pathinfo($uri, PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
}
/** /**
* Get the request method. * Get the request method.
* *

View File

@ -104,71 +104,38 @@ public function find($name)
* *
* @param string $method * @param string $method
* @param string $uri * @param string $uri
* @param string $format
* @return Route * @return Route
*/ */
public function route($method, $uri, $format) public function route($method, $uri)
{ {
$routes = $this->loader->load($uri); $routes = $this->loader->load($uri);
// Put the request method and URI in route form. Routes begin with // All route URIs begin with the request method and have a leading
// the request method and a forward slash followed by the URI. // slash before the URI. We'll put the request method and URI into
// that format so we can easily check for literal matches.
$destination = $method.' /'.trim($uri, '/'); $destination = $method.' /'.trim($uri, '/');
// Check for a literal route match first...
if (isset($routes[$destination])) if (isset($routes[$destination]))
{ {
return Request::$route = new Route($destination, $routes[$destination], array()); return new Route($destination, $routes[$destination], array());
} }
// If no literal route match was found, we will iterate through all
// of the routes and check each of them one at a time, translating
// any wildcards in the route into actual regular expressions.
foreach ($routes as $keys => $callback) foreach ($routes as $keys => $callback)
{ {
$formats = $this->formats($callback);
// Only check the routes that couldn't be matched literally... // Only check the routes that couldn't be matched literally...
if (($format_count = count($formats)) > 0 or $this->fuzzy($keys)) if (strpos($keys, '(') !== false or strpos($keys, ',') !== false)
{ {
if ($format_count > 0 and ! in_array($format, $formats)) continue; if ( ! is_null($route = $this->match($destination, $keys, $callback)))
if ( ! is_null($route = $this->match($destination, $keys, $callback, $format)))
{ {
return Request::$route = $route; return $route;
} }
} }
} }
return Request::$route = $this->controller($method, $uri, $destination); return $this->controller($method, $uri, $destination);
}
/**
* Get the request formats for which the route provides responses.
*
* @param mixed $callback
* @return array
*/
protected function formats($callback)
{
if (is_array($callback) and isset($callback['provides']))
{
return (is_string($provides = $callback['provides'])) ? explode('|', $provides) : $provides;
}
return array();
}
/**
* Determine if a route needs to be examined using a regular expression.
*
* Routes that contain wildcards or multiple URIs cannot be matched using
* a literal key check on the array. The wildcards will have to be turned
* into real regular expressions and the multiple URIs have to be split.
*
* @param string $keys
* @return bool
*/
protected function fuzzy($keys)
{
return strpos($keys, '(') !== false or strpos($keys, ',') !== false;
} }
/** /**
@ -181,16 +148,10 @@ protected function fuzzy($keys)
* @param string $destination * @param string $destination
* @param array $keys * @param array $keys
* @param mixed $callback * @param mixed $callback
* @param string $format
* @return mixed * @return mixed
*/ */
protected function match($destination, $keys, $callback, $format) protected function match($destination, $keys, $callback)
{ {
// We need to remove the format from the route since formats are
// not specified in the route URI directly, but rather through
// the "provides" keyword on the route array.
$destination = str_replace('.'.$format, '', $destination);
foreach (explode(', ', $keys) as $key) foreach (explode(', ', $keys) as $key)
{ {
if (preg_match('#^'.$this->wildcards($key).'$#', $destination)) if (preg_match('#^'.$this->wildcards($key).'$#', $destination))
@ -257,6 +218,22 @@ protected function controller_key($segments)
} }
} }
/**
* Get the request formats for which the route provides responses.
*
* @param mixed $callback
* @return array
*/
protected function formats($callback)
{
if (is_array($callback) and isset($callback['provides']))
{
return (is_string($provides = $callback['provides'])) ? explode('|', $provides) : $provides;
}
return array('html');
}
/** /**
* Translate route URI wildcards into actual regular expressions. * Translate route URI wildcards into actual regular expressions.
* *
@ -288,11 +265,6 @@ protected function wildcards($key)
*/ */
protected function parameters($uri, $route) protected function parameters($uri, $route)
{ {
// When gathering the parameters, we need to get the request format out
// of the destination, otherwise it could be passed in as a parameter
// to the route closure or controller, which we don't want.
$uri = str_replace('.'.Request::format(), '', $uri);
list($uri, $route) = array(explode('/', $uri), explode('/', $route)); list($uri, $route) = array(explode('/', $uri), explode('/', $route));
$count = count($route); $count = count($route);

View File

@ -31,12 +31,6 @@ public function test_request_method_returns_request_method_from_server_array()
$this->assertEquals('PUT', Laravel\Request::method()); $this->assertEquals('PUT', Laravel\Request::method());
} }
public function test_format_method_returns_extension()
{
$this->assertEquals('html', Laravel\Request::format('user'));
$this->assertEquals('json', Laravel\Request::format('user.json'));
}
public function test_server_method_returns_from_the_server_array() public function test_server_method_returns_from_the_server_array()
{ {
$_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor')); $_SERVER = array('TEST' => 'something', 'USER' => array('NAME' => 'taylor'));