From 395236d6433597ca414e710a6900156e7119779b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 5 Feb 2012 19:57:34 -0600 Subject: [PATCH] support universal routes. --- laravel/routing/router.php | 40 +++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/laravel/routing/router.php b/laravel/routing/router.php index d67451f7..3623f6d0 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -36,6 +36,13 @@ class Router { '/(: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. * @@ -48,13 +55,20 @@ class Router { * * * @param string|array $route - * @param string $action + * @param mixed $action * @return void */ public static function register($route, $action) { 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 // 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 @@ -78,6 +92,30 @@ public static function register($route, $action) } } + /** + * 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. *