Commit 395236d6 authored by Taylor Otwell's avatar Taylor Otwell

support universal routes.

parent df3a277b
...@@ -36,6 +36,13 @@ class Router { ...@@ -36,6 +36,13 @@ class Router {
'/(:any?)' => '(?:/([a-zA-Z0-9\.\-_%]+)', '/(:any?)' => '(?:/([a-zA-Z0-9\.\-_%]+)',
); );
/**
* An array of HTTP request methods.
*
* @var array
*/
public static $methods = array('GET', 'POST', 'PUT', 'DELETE');
/** /**
* Register a route with the router. * Register a route with the router.
* *
...@@ -48,13 +55,20 @@ class Router { ...@@ -48,13 +55,20 @@ class Router {
* </code> * </code>
* *
* @param string|array $route * @param string|array $route
* @param string $action * @param mixed $action
* @return void * @return void
*/ */
public static function register($route, $action) public static function register($route, $action)
{ {
foreach ((array) $route as $uri) foreach ((array) $route as $uri)
{ {
if (starts_with($uri, '*'))
{
static::universal(substr($uri, 2), $action);
continue;
}
// If the action is a string, it is a pointer to a controller, so we // If the action is a string, it is a pointer to a controller, so we
// need to add it to the action array as a "uses" clause, which will // need to add it to the action array as a "uses" clause, which will
// indicate to the route to call the controller when the route is // indicate to the route to call the controller when the route is
...@@ -78,6 +92,30 @@ class Router { ...@@ -78,6 +92,30 @@ class Router {
} }
} }
/**
* Register a route for all HTTP verbs.
*
* @param string $route
* @param mixed $action
* @return void
*/
protected static function universal($route, $action)
{
$count = count(static::$methods);
$routes = array_fill(0, $count, $route);
// When registering a universal route, we'll iterate through all of the
// verbs supported by the router and prepend each one of the URIs with
// one of the request verbs, then we'll register the routes.
for ($i = 0; $i < $count; $i++)
{
$routes[$i] = static::$methods[$i].' '.$routes[$i];
}
static::register($routes, $action);
}
/** /**
* Find a route by the route's assigned name. * Find a route by the route's assigned name.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment