diff --git a/README.markdown b/README.markdown index 35ded6b..66b4ec9 100644 --- a/README.markdown +++ b/README.markdown @@ -158,19 +158,14 @@ Defaults to `twig`. Will be appended to template name given in `render()` method // how to integrate the Slim middleware $app->addRoutes(array( - '/' => array( - 'Home:index', - function() { - error_log("MIDDLWARE FOR SINGLE ROUTE"); + '/' => array('Home:index', array(function() { + error_log("MIDDLEWARE FOR SINGLE ROUTE"); }, function() { - error_log("ADDITIONAL MIDDLWARE FOR SINGLE ROUTE"); - } + error_log("ADDITIONAL MIDDLEWARE FOR SINGLE ROUTE"); + }) ), - '/hello/:name' => array( - 'Home:hello', - 'post', - function() { + '/hello/:name' => array('post' => 'Home:hello', function() { error_log("THIS ROUTE IS ONLY POST"); } ) diff --git a/src/SlimController/Slim.php b/src/SlimController/Slim.php index 31e2c58..d74297a 100644 --- a/src/SlimController/Slim.php +++ b/src/SlimController/Slim.php @@ -23,7 +23,7 @@ class Slim extends \Slim\Slim /** * @var array */ - protected static $ALLOWED_HTTP_METHODS = array('GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'); + protected static $ALLOWED_HTTP_METHODS = array('ANY', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'); /** * Add multiple controller based routes @@ -57,43 +57,47 @@ class Slim extends \Slim\Slim * ), function() {}); * * - * @param array $routes The route definitions - * @param callable,... $middlewares Optional callable used for all routes as middleware - * + * @param array $routes The route definitions + * @param array $globalMiddlewares * @throws \InvalidArgumentException + * @internal param $callable ,... $middlewares Optional callable used for all routes as middleware + * * @return $this */ - public function addRoutes(array $routes, $middlewares = null) + public function addRoutes(array $routes, $globalMiddlewares = array()) { - $args = func_get_args(); - array_shift($args); - $middlewares = $args; + if (!is_array($globalMiddlewares)) { + $globalMiddlewares = array($globalMiddlewares); + }; foreach ($routes as $path => $routeArgs) { - $httpMethod = 'any'; - - // simple - if (!is_array($routeArgs)) { - $routeArgs = array($routeArgs); - } - - $classRoute = array_shift($routeArgs); + // create array for simple request + $routeArgs = (is_array($routeArgs)) ? $routeArgs : array('any' => $routeArgs); + + foreach ($routeArgs as $httpMethod => $classArgs) { + // assign vars if middleware callback exists + if(is_array($classArgs)) { + $classRoute = $classArgs[0]; + $localMiddlewares = (is_array($classArgs[1])) ? $classArgs[1] : array($classArgs[1]); + } else { + $classRoute = $classArgs; + $localMiddlewares = array(); + } - // specific HTTP method - if (count($routeArgs) > 0 && is_string($routeArgs[0])) { - $httpMethod = strtoupper(array_shift($routeArgs)); + // specific HTTP method + $httpMethod = strtoupper($httpMethod); if (!in_array($httpMethod, static::$ALLOWED_HTTP_METHODS)) { throw new \InvalidArgumentException("Http method '$httpMethod' is not supported."); } - } - $routeMiddlewares = array_merge($routeArgs, $middlewares); - $route = $this->addControllerRoute($path, $classRoute, $routeMiddlewares)->name($classRoute); + $routeMiddlewares = array_merge($localMiddlewares, $globalMiddlewares); + $route = $this->addControllerRoute($path, $classRoute, $routeMiddlewares)->name($classRoute); - if ('any' === $httpMethod) { - call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS); - } else { - $route->via($httpMethod); + if ('any' === $httpMethod) { + call_user_func_array(array($route, 'via'), static::$ALLOWED_HTTP_METHODS); + } else { + $route->via($httpMethod); + } } } diff --git a/tests/SlimController/Tests/SlimTest.php b/tests/SlimController/Tests/SlimTest.php index 9f591c2..ef7aeee 100644 --- a/tests/SlimController/Tests/SlimTest.php +++ b/tests/SlimController/Tests/SlimTest.php @@ -41,7 +41,7 @@ public function testAddRoutesInExtendedFormat() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('Controller:index', 'get') + '/bla' => array('get' => 'Controller:index') )); $this->assertEquals(1, count($this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()))); } @@ -80,9 +80,9 @@ public function testLocalMiddlewareIsAddedToRoute() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('Controller:index', function() { + '/bla' => array('get' => array('Controller:index', function() { return false; - }) + })) )); /** @var \Slim\Route[] $routes */ @@ -94,18 +94,36 @@ public function testLocalMiddlewareIsAddedToRoute() $this->assertSame(1, count($middleware)); } + public function testArrayOfLocalMiddlewareIsAddedToRoute() + { + $this->setUrl('/bla'); + $this->app->addRoutes(array( + '/bla' => array('get' => array('Controller:index', [function() { + return false; + }, function() { return false; } ])) + )); + + /** @var \Slim\Route[] $routes */ + $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); + $this->assertEquals(1, count($routes)); + + $middleware = $routes[0]->getMiddleware(); + $this->assertInternalType('array', $middleware); + $this->assertSame(2, count($middleware)); + } + public function testGlobalAndLocalMiddlewareIsAddedToRoute() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('Controller:index', function() { + '/bla' => array('get' => array('Controller:index', function() { return false; - }) - ), function() { + })) + ), array(function() { return false; }, function() { return false; - }); + })); /** @var \Slim\Route[] $routes */ $routes = $this->app->router()->getMatchedRoutes($this->req->getMethod(), $this->req->getResourceUri()); @@ -124,7 +142,7 @@ public function testFailToAddRouteForUnsupportedHttpMethod() { $this->setUrl('/bla'); $this->app->addRoutes(array( - '/bla' => array('Controller:index', 'foo') + '/bla' => array('foo' => 'Controller:index') )); }