From b415b2accb3acb11cc138dc97504132ed56d269f Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 12 Apr 2014 02:17:38 -0500 Subject: [PATCH 1/3] Fix matching prefix for api routes --- src/Routing/ApiCollection.php | 38 ++++++++++++++++++++++++++++------- tests/RoutingRouterTest.php | 28 +++++++++++++++++--------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/Routing/ApiCollection.php b/src/Routing/ApiCollection.php index 3f318ab2d..c708e0c4a 100644 --- a/src/Routing/ApiCollection.php +++ b/src/Routing/ApiCollection.php @@ -6,21 +6,21 @@ class ApiCollection extends RouteCollection { /** * API version. - * + * * @var string */ protected $version; /** * API options. - * + * * @var array */ protected $options; /** * Create a new dispatcher instance. - * + * * @param string $version * @param array $options * @return void @@ -33,7 +33,7 @@ public function __construct($version, array $options) /** * Get an option from the collection. - * + * * @param string $key * @param mixed $default * @return mixed @@ -46,17 +46,17 @@ public function option($key, $default = null) /** * Determine if the routes within the collection will be a match for * the current request. - * + * * @param \Illuminate\Http\Request $request * @return bool */ public function matches($request) { - if ($this->option('domain') and $request->header('host') == $this->option('domain')) + if ($this->matchDomain($request)) { return true; } - elseif ($this->option('prefix') and starts_with($request->getPathInfo(), trim($this->option('prefix'), '/'))) + elseif ($this->matchPrefix($request)) { return true; } @@ -68,4 +68,28 @@ public function matches($request) return false; } + /** + * Matches domain name if is set on route group + * + * @param $request + * @return bool + */ + protected function matchDomain($request) + { + return $this->option('domain') and $request->header('host') == $this->option('domain'); + } + + /** + * Matches prefix if is set in route group + * + * @param $request + * @return bool + */ + protected function matchPrefix($request) + { + $path = explode('/', trim($request->getPathInfo(), '/')); + + return ($path[0] === $this->option('prefix') and $this->option('prefix')); + } + } \ No newline at end of file diff --git a/tests/RoutingRouterTest.php b/tests/RoutingRouterTest.php index 5018bcb43..152cc4c01 100644 --- a/tests/RoutingRouterTest.php +++ b/tests/RoutingRouterTest.php @@ -61,19 +61,29 @@ public function testRegisteringApiRouteGroupWithoutVersionThrowsException() $this->router->api([], function(){}); } - - public function testRouterDispatchesInternalRequests() + public function testPrefixOnApiRoutes() { - $this->router->api(['version' => 'v1'], function() - { - $this->router->get('foo', function() { return 'bar'; }); - }); - - $this->assertEquals('{"message":"bar"}', $this->router->dispatch(Dingo\Api\Http\InternalRequest::create('foo', 'GET'))->getContent()); + $this->router->api(['version' => 'v1', 'prefix' => 'api'], function() + { + $this->router->get('foo', function() { return 'bar'; }); + }); + + $route = $this->router->getApiCollection('v1')->getRoutes()[0]; + + $this->assertEquals('api', $route->getAction()['prefix']); } + public function testRouterDispatchesInternalRequests() + { + $this->router->api(['version' => 'v1'], function() + { + $this->router->get('foo', function() { return 'bar'; }); + }); + + $this->assertEquals('{"message":"bar"}', $this->router->dispatch(Dingo\Api\Http\InternalRequest::create('foo', 'GET'))->getContent()); + } - public function testRouterFindsCollectionCurrentRequestIsTargetting() + public function testRouterFindsCollectionCurrentRequestIsTargeting() { $this->router->api(['version' => 'v1'], function() { From 05e30788477a8bd5fda73389b148441bcdb62a2b Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 12 Apr 2014 03:27:49 -0500 Subject: [PATCH 2/3] Change spaces to tabs --- src/Routing/ApiCollection.php | 42 +++++++++++++++++------------------ tests/RoutingRouterTest.php | 28 +++++++++++------------ 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Routing/ApiCollection.php b/src/Routing/ApiCollection.php index c708e0c4a..d466a76f9 100644 --- a/src/Routing/ApiCollection.php +++ b/src/Routing/ApiCollection.php @@ -68,28 +68,28 @@ public function matches($request) return false; } - /** - * Matches domain name if is set on route group - * - * @param $request - * @return bool - */ - protected function matchDomain($request) - { - return $this->option('domain') and $request->header('host') == $this->option('domain'); - } + /** + * Matches domain name if is set on route group + * + * @param $request + * @return bool + */ + protected function matchDomain($request) + { + return $this->option('domain') and $request->header('host') == $this->option('domain'); + } - /** - * Matches prefix if is set in route group - * - * @param $request - * @return bool - */ - protected function matchPrefix($request) - { - $path = explode('/', trim($request->getPathInfo(), '/')); + /** + * Matches prefix if is set in route group + * + * @param $request + * @return bool + */ + protected function matchPrefix($request) + { + $path = explode('/', trim($request->getPathInfo(), '/')); - return ($path[0] === $this->option('prefix') and $this->option('prefix')); - } + return ($path[0] === $this->option('prefix') and $this->option('prefix')); + } } \ No newline at end of file diff --git a/tests/RoutingRouterTest.php b/tests/RoutingRouterTest.php index 152cc4c01..64089a591 100644 --- a/tests/RoutingRouterTest.php +++ b/tests/RoutingRouterTest.php @@ -63,25 +63,25 @@ public function testRegisteringApiRouteGroupWithoutVersionThrowsException() public function testPrefixOnApiRoutes() { - $this->router->api(['version' => 'v1', 'prefix' => 'api'], function() - { - $this->router->get('foo', function() { return 'bar'; }); - }); + $this->router->api(['version' => 'v1', 'prefix' => 'api'], function() + { + $this->router->get('foo', function() { return 'bar'; }); + }); - $route = $this->router->getApiCollection('v1')->getRoutes()[0]; + $route = $this->router->getApiCollection('v1')->getRoutes()[0]; - $this->assertEquals('api', $route->getAction()['prefix']); + $this->assertEquals('api', $route->getAction()['prefix']); } - public function testRouterDispatchesInternalRequests() - { - $this->router->api(['version' => 'v1'], function() - { - $this->router->get('foo', function() { return 'bar'; }); - }); + public function testRouterDispatchesInternalRequests() + { + $this->router->api(['version' => 'v1'], function() + { + $this->router->get('foo', function() { return 'bar'; }); + }); - $this->assertEquals('{"message":"bar"}', $this->router->dispatch(Dingo\Api\Http\InternalRequest::create('foo', 'GET'))->getContent()); - } + $this->assertEquals('{"message":"bar"}', $this->router->dispatch(Dingo\Api\Http\InternalRequest::create('foo', 'GET'))->getContent()); + } public function testRouterFindsCollectionCurrentRequestIsTargeting() { From 1548080b01f77e642f0b60ab025435e9da6ab398 Mon Sep 17 00:00:00 2001 From: Joseph Cohen Date: Sat, 12 Apr 2014 04:13:58 -0500 Subject: [PATCH 3/3] Added slashed prefixes --- src/Routing/ApiCollection.php | 22 ++++++++++++++++++++-- tests/RoutingRouterTest.php | 4 ++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Routing/ApiCollection.php b/src/Routing/ApiCollection.php index d466a76f9..ae5bde263 100644 --- a/src/Routing/ApiCollection.php +++ b/src/Routing/ApiCollection.php @@ -87,9 +87,27 @@ protected function matchDomain($request) */ protected function matchPrefix($request) { - $path = explode('/', trim($request->getPathInfo(), '/')); + if ( ! $prefix = $this->option('prefix')) + { + return false; + } + + $prefix = $this->filterAndExplode($this->option('prefix')); - return ($path[0] === $this->option('prefix') and $this->option('prefix')); + $path = $this->filterAndExplode($request->getPathInfo()); + + return $prefix == array_slice($path, 0, count($prefix)); + } + + /** + * Explode array on slash and remove empty values. + * + * @param array $array + * @return array + */ + protected function filterAndExplode($array) + { + return array_filter(explode('/', $array)); } } \ No newline at end of file diff --git a/tests/RoutingRouterTest.php b/tests/RoutingRouterTest.php index 64089a591..fff9582a5 100644 --- a/tests/RoutingRouterTest.php +++ b/tests/RoutingRouterTest.php @@ -63,14 +63,14 @@ public function testRegisteringApiRouteGroupWithoutVersionThrowsException() public function testPrefixOnApiRoutes() { - $this->router->api(['version' => 'v1', 'prefix' => 'api'], function() + $this->router->api(['version' => 'v1', 'prefix' => 'foo/bar'], function() { $this->router->get('foo', function() { return 'bar'; }); }); $route = $this->router->getApiCollection('v1')->getRoutes()[0]; - $this->assertEquals('api', $route->getAction()['prefix']); + $this->assertEquals('foo/bar', $route->getAction()['prefix']); } public function testRouterDispatchesInternalRequests()