Skip to content

Fix matching prefix for api routes #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions src/Routing/ApiCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +33,7 @@ public function __construct($version, array $options)

/**
* Get an option from the collection.
*
*
* @param string $key
* @param mixed $default
* @return mixed
Expand All @@ -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;
}
Expand All @@ -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));
}

}
16 changes: 13 additions & 3 deletions tests/RoutingRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,29 @@ 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()
{
$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()
{
Expand Down