Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/Routing/Adapter/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Laravel implements Adapter
*/
protected $oldRoutes;

/**
* The globally available parameter patterns.
*
* @var array
*/
protected $patterns = [];

/**
* Create a new laravel routing adapter instance.
*
Expand Down Expand Up @@ -245,4 +252,14 @@ public function getRouter()
{
return $this->router;
}

/**
* Get the global "where" patterns.
*
* @return array
*/
public function getPatterns()
{
return $this->patterns;
}
}
31 changes: 31 additions & 0 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Illuminate\Container\Container;
use Dingo\Api\Contract\Routing\Adapter;
use Dingo\Api\Contract\Debug\ExceptionHandler;
use Illuminate\Routing\Route as IlluminateRoute;
use Illuminate\Http\Response as IlluminateResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class Router
Expand Down Expand Up @@ -622,6 +624,17 @@ public function getCurrentRoute()
return;
}

// We need to recompile the route, adding the where clause (for pattern restrictions) and check again
if (is_object($route) && $route instanceof IlluminateRoute) {
$route->compiled = false;
$this->addWhereClausesToRoute($route);

// If the matching fails, it would be due to a parameter format validation check fail
if (! $route->matches($this->container['request'])) {
throw new NotFoundHttpException('Not Found!');
}
}

return $this->currentRoute = $this->createRoute($route);
}

Expand Down Expand Up @@ -844,4 +857,22 @@ public function currentRouteUses($action)
{
return $this->currentRouteAction() == $action;
}

/**
* Add the necessary where clauses to the route based on its initial registration.
*
* @param \Illuminate\Routing\Route $route
*
* @return \Illuminate\Routing\Route
*/
protected function addWhereClausesToRoute($route)
{
$patterns = app()->make(\Illuminate\Routing\Router::class)->getPatterns();

$route->where(array_merge(
$patterns, $route->getAction()['where'] ?? []
));

return $route;
}
}
2 changes: 2 additions & 0 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function setUp()
$this->auth = new Auth($this->router, $this->container, []);
$this->dispatcher = new Dispatcher($this->container, new Filesystem, $this->router, $this->auth);

app()->instance(\Illuminate\Routing\Router::class, $this->adapter, true);

$this->dispatcher->setSubtype('api');
$this->dispatcher->setStandardsTree('vnd');
$this->dispatcher->setDefaultVersion('v1');
Expand Down
1 change: 1 addition & 0 deletions tests/Routing/Adapter/BaseAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function setUp()
$this->adapter = $this->getAdapterInstance();
$this->exception = m::mock(\Dingo\Api\Exception\Handler::class);
$this->router = new Router($this->adapter, $this->exception, $this->container, null, null);
app()->instance(\Illuminate\Routing\Router::class, $this->adapter, true);

Http\Response::setFormatters(['json' => new Http\Response\Format\Json]);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Stubs/RoutingAdapterStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public function pattern($key, $pattern)
$this->patterns[$key] = $pattern;
}

public function getPatterns()
{
return $this->patterns;
}

protected function createRouteCollections(array $versions)
{
foreach ($versions as $version) {
Expand Down