Skip to content

Commit e718128

Browse files
Merge pull request dingo#1694 from christoph-kluge/handling-for-duplicate-routes-with-different-patterns
Add Route::patterns() while app is beeing bootstrapped and not after
2 parents 512eb1a + 4d738b2 commit e718128

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed

src/Routing/Adapter/Laravel.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Dingo\Api\Routing\Adapter;
44

5+
use Dingo\Api\Contract\Routing\Adapter;
6+
use Dingo\Api\Exception\UnknownVersionException;
57
use Illuminate\Http\Request;
68
use Illuminate\Routing\Route;
7-
use Illuminate\Routing\Router;
8-
use Dingo\Api\Contract\Routing\Adapter;
99
use Illuminate\Routing\RouteCollection;
10-
use Dingo\Api\Exception\UnknownVersionException;
10+
use Illuminate\Routing\Router;
1111

1212
class Laravel implements Adapter
1313
{
@@ -147,6 +147,9 @@ public function addRoute(array $methods, array $versions, $uri, $action)
147147
{
148148
$this->createRouteCollections($versions);
149149

150+
// Add where-patterns from original laravel router
151+
$action['where'] = array_merge($this->router->getPatterns(), $action['where'] ?? []);
152+
150153
$route = new Route($methods, $uri, $action);
151154
$route->where($action['where']);
152155

src/Routing/Router.php

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
namespace Dingo\Api\Routing;
44

55
use Closure;
6-
use Exception;
7-
use RuntimeException;
6+
use Dingo\Api\Contract\Debug\ExceptionHandler;
7+
use Dingo\Api\Contract\Routing\Adapter;
8+
use Dingo\Api\Http\InternalRequest;
89
use Dingo\Api\Http\Request;
9-
use Illuminate\Support\Arr;
10-
use Illuminate\Support\Str;
1110
use Dingo\Api\Http\Response;
12-
use Illuminate\Http\JsonResponse;
13-
use Dingo\Api\Http\InternalRequest;
11+
use Exception;
1412
use Illuminate\Container\Container;
15-
use Dingo\Api\Contract\Routing\Adapter;
16-
use Dingo\Api\Contract\Debug\ExceptionHandler;
17-
use Illuminate\Routing\Route as IlluminateRoute;
13+
use Illuminate\Http\JsonResponse;
1814
use Illuminate\Http\Response as IlluminateResponse;
19-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
15+
use Illuminate\Support\Arr;
16+
use Illuminate\Support\Str;
17+
use RuntimeException;
2018
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
2119

2220
class Router
@@ -624,17 +622,6 @@ public function getCurrentRoute()
624622
return;
625623
}
626624

627-
// We need to recompile the route, adding the where clause (for pattern restrictions) and check again
628-
if (is_object($route) && $route instanceof IlluminateRoute) {
629-
$route->compiled = false;
630-
$this->addWhereClausesToRoute($route);
631-
632-
// If the matching fails, it would be due to a parameter format validation check fail
633-
if (! $route->matches($this->container['request'])) {
634-
throw new NotFoundHttpException('Not Found!');
635-
}
636-
}
637-
638625
return $this->currentRoute = $this->createRoute($route);
639626
}
640627

@@ -857,22 +844,4 @@ public function currentRouteUses($action)
857844
{
858845
return $this->currentRouteAction() == $action;
859846
}
860-
861-
/**
862-
* Add the necessary where clauses to the route based on its initial registration.
863-
*
864-
* @param \Illuminate\Routing\Route $route
865-
*
866-
* @return \Illuminate\Routing\Route
867-
*/
868-
protected function addWhereClausesToRoute($route)
869-
{
870-
$patterns = app()->make(\Illuminate\Routing\Router::class)->getPatterns();
871-
872-
$route->where(array_merge(
873-
$patterns, $route->getAction()['where'] ?? []
874-
));
875-
876-
return $route;
877-
}
878847
}

0 commit comments

Comments
 (0)