Skip to content

Commit e2ac94f

Browse files
committed
[Feature] Pass Laravel router instance to routing callbacks
The Laravel router instance is now passed through to the resources, relationships and actions callbacks as the second function argument. Closes #9
1 parent cd8e60b commit e2ac94f

File tree

7 files changed

+17
-7
lines changed

7 files changed

+17
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ All notable changes to this project will be documented in this file. This projec
2525
can be used to specify the named server the response should use to encode the JSON:API document. This has to be used
2626
when returning responses from routes that have not run the JSON:API middleware (i.e. there is no default server
2727
available via the service container).
28+
- [#9](https://github.com/laravel-json-api/laravel/issues/9) The Laravel route registrar is now passed through to
29+
the `resources`, `relationships` and `actions` callbacks as the second function argument.
2830

2931
### Changed
3032

src/Routing/PendingServerRegistration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function resources(\Closure $callback): void
145145
$callback(new ResourceRegistrar(
146146
$this->router,
147147
$this->server
148-
));
148+
), $this->router);
149149
});
150150
}
151151
}

src/Routing/ResourceRegistrar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function relationships(
9999
$this->router->group($attributes, function () use ($registrar, $callback, $routes) {
100100
$relationships = new Relationships($registrar);
101101

102-
$callback($relationships);
102+
$callback($relationships, $this->router);
103103

104104
foreach ($relationships->register() as $route) {
105105
$routes->add($route);
@@ -140,7 +140,7 @@ public function actions(
140140
);
141141

142142
$this->router->group($attributes, function () use ($actions, $callback) {
143-
$callback($actions);
143+
$callback($actions, $this->router);
144144
});
145145

146146
return $routes;

tests/lib/Integration/Routing/ActionsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace LaravelJsonApi\Laravel\Tests\Integration\Routing;
2121

22+
use Illuminate\Contracts\Routing\Registrar;
2223
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
2324

2425
class ActionsTest extends TestCase
@@ -54,7 +55,8 @@ public function testBase(string $method): void
5455
->prefix('v1')
5556
->namespace('Api\\V1')
5657
->resources(function ($server) use ($func) {
57-
$server->resource('posts')->actions(function ($actions) use ($func) {
58+
$server->resource('posts')->actions(function ($actions, $routes) use ($func) {
59+
$this->assertInstanceOf(Registrar::class, $routes);
5860
$actions->{$func}('foo-bar');
5961
});
6062
});

tests/lib/Integration/Routing/HasManyTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace LaravelJsonApi\Laravel\Tests\Integration\Routing;
2121

22+
use Illuminate\Contracts\Routing\Registrar;
2223
use LaravelJsonApi\Core\Support\Arr;
2324
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
2425

@@ -79,7 +80,8 @@ public function test(string $method, string $uri, string $action, string $name):
7980

8081
$this->defaultApiRoutesWithNamespace(function () {
8182
JsonApiRoute::server('v1')->prefix('v1')->namespace('Api\\V1')->resources(function ($server) {
82-
$server->resource('posts')->relationships(function ($relations) {
83+
$server->resource('posts')->relationships(function ($relations, $routes) {
84+
$this->assertInstanceOf(Registrar::class, $routes);
8385
$relations->hasMany('tags');
8486
});
8587
});

tests/lib/Integration/Routing/HasOneTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
namespace LaravelJsonApi\Laravel\Tests\Integration\Routing;
2121

22+
use Illuminate\Contracts\Routing\Registrar;
2223
use LaravelJsonApi\Core\Support\Arr;
2324
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
2425

@@ -67,7 +68,8 @@ public function test(string $method, string $uri, string $action, string $name):
6768

6869
$this->defaultApiRoutesWithNamespace(function () {
6970
JsonApiRoute::server('v1')->prefix('v1')->namespace('Api\\V1')->resources(function ($server) {
70-
$server->resource('posts')->relationships(function ($relations) {
71+
$server->resource('posts')->relationships(function ($relations, $routes) {
72+
$this->assertInstanceOf(Registrar::class, $routes);
7173
$relations->hasOne('author');
7274
});
7375
});

tests/lib/Integration/Routing/ResourceTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace LaravelJsonApi\Laravel\Tests\Integration\Routing;
2121

2222
use App\Http\Controllers\Api\V1\PostController;
23+
use Illuminate\Contracts\Routing\Registrar;
2324
use LaravelJsonApi\Core\Support\Arr;
2425
use LaravelJsonApi\Laravel\Facades\JsonApiRoute;
2526

@@ -53,7 +54,8 @@ public function test(string $method, string $uri, string $action, bool $id): voi
5354
$this->createSchema($server, 'posts', '\d+');
5455

5556
$this->defaultApiRoutesWithNamespace(function () {
56-
JsonApiRoute::server('v1')->prefix('v1')->namespace('Api\\V1')->resources(function ($server) {
57+
JsonApiRoute::server('v1')->prefix('v1')->namespace('Api\\V1')->resources(function ($server, $routes) {
58+
$this->assertInstanceOf(Registrar::class, $routes);
5759
$server->resource('posts');
5860
});
5961
});

0 commit comments

Comments
 (0)