diff --git a/src/Routing/ApiCollection.php b/src/Routing/ApiCollection.php index 3f318ab2d..ae5bde263 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,46 @@ 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) + { + if ( ! $prefix = $this->option('prefix')) + { + return false; + } + + $prefix = $this->filterAndExplode($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 5018bcb43..fff9582a5 100644 --- a/tests/RoutingRouterTest.php +++ b/tests/RoutingRouterTest.php @@ -61,6 +61,17 @@ public function testRegisteringApiRouteGroupWithoutVersionThrowsException() $this->router->api([], function(){}); } + public function testPrefixOnApiRoutes() + { + $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('foo/bar', $route->getAction()['prefix']); + } public function testRouterDispatchesInternalRequests() { @@ -68,12 +79,11 @@ public function testRouterDispatchesInternalRequests() { $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() {