Skip to content

Commit

Permalink
Merge pull request #1694 from christoph-kluge/handling-for-duplicate-…
Browse files Browse the repository at this point in the history
…routes-with-different-patterns

Add Route::patterns() while app is beeing bootstrapped and not after
  • Loading branch information
specialtactics authored Jan 3, 2020
2 parents 512eb1a + 4d738b2 commit e718128
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 42 deletions.
9 changes: 6 additions & 3 deletions src/Routing/Adapter/Laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Dingo\Api\Routing\Adapter;

use Dingo\Api\Contract\Routing\Adapter;
use Dingo\Api\Exception\UnknownVersionException;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Dingo\Api\Contract\Routing\Adapter;
use Illuminate\Routing\RouteCollection;
use Dingo\Api\Exception\UnknownVersionException;
use Illuminate\Routing\Router;

class Laravel implements Adapter
{
Expand Down Expand Up @@ -147,6 +147,9 @@ public function addRoute(array $methods, array $versions, $uri, $action)
{
$this->createRouteCollections($versions);

// Add where-patterns from original laravel router
$action['where'] = array_merge($this->router->getPatterns(), $action['where'] ?? []);

$route = new Route($methods, $uri, $action);
$route->where($action['where']);

Expand Down
47 changes: 8 additions & 39 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@
namespace Dingo\Api\Routing;

use Closure;
use Exception;
use RuntimeException;
use Dingo\Api\Contract\Debug\ExceptionHandler;
use Dingo\Api\Contract\Routing\Adapter;
use Dingo\Api\Http\InternalRequest;
use Dingo\Api\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Dingo\Api\Http\Response;
use Illuminate\Http\JsonResponse;
use Dingo\Api\Http\InternalRequest;
use Exception;
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\JsonResponse;
use Illuminate\Http\Response as IlluminateResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use RuntimeException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class Router
Expand Down Expand Up @@ -624,17 +622,6 @@ 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 @@ -857,22 +844,4 @@ 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;
}
}

0 comments on commit e718128

Please sign in to comment.