Skip to content

Commit de7bbb4

Browse files
committed
Inject a new router instance into the Laravel adapter.
Instead of using the existing Laravel router we will use a new instance. This should prevent the APIs internal dispatcher from overriding the current route on the actual Laravel router instance. There does not seem to be any need for the router instance that is injected into the adapter to be the actual Laravel router, as the adapter just needs an instance to dispatch the API-only routes.
1 parent 7fa939c commit de7bbb4

File tree

2 files changed

+3
-31
lines changed

2 files changed

+3
-31
lines changed

src/Provider/LaravelServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Dingo\Api\Provider;
44

55
use ReflectionClass;
6+
use Illuminate\Routing\Router;
67
use Illuminate\Contracts\Http\Kernel;
78
use Dingo\Api\Routing\Adapter\Laravel as LaravelAdapter;
89

@@ -41,7 +42,7 @@ public function register()
4142
$this->addRequestMiddlewareToBeginning($kernel);
4243

4344
$this->app->singleton('api.router.adapter', function ($app) {
44-
return new LaravelAdapter($app['router']);
45+
return new LaravelAdapter(new Router($app['events'], $app));
4546
});
4647
}
4748

src/Routing/Adapter/Laravel.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ class Laravel implements Adapter
2525
*/
2626
protected $routes = [];
2727

28-
/**
29-
* Old routes already defined on the router.
30-
*
31-
* @var \Illuminate\Routing\RouteCollection
32-
*/
33-
protected $oldRoutes;
34-
3528
/**
3629
* Create a new laravel routing adapter instance.
3730
*
@@ -58,33 +51,11 @@ public function dispatch(Request $request, $version)
5851
throw new UnknownVersionException;
5952
}
6053

61-
$routes = $this->mergeExistingRoutes($this->routes[$version]);
62-
63-
$this->router->setRoutes($routes);
54+
$this->router->setRoutes($this->routes[$version]);
6455

6556
return $this->router->dispatch($request);
6657
}
6758

68-
/**
69-
* Merge the existing routes with the new routes.
70-
*
71-
* @param \Illuminate\Routing\RouteCollection $routes
72-
*
73-
* @return \Illuminate\Routing\RouteCollection
74-
*/
75-
protected function mergeExistingRoutes(RouteCollection $routes)
76-
{
77-
if (! isset($this->oldRoutes)) {
78-
$this->oldRoutes = $this->router->getRoutes();
79-
}
80-
81-
foreach ($this->oldRoutes as $route) {
82-
$routes->add($route);
83-
}
84-
85-
return $routes;
86-
}
87-
8859
/**
8960
* Get the URI, methods, and action from the route.
9061
*

0 commit comments

Comments
 (0)