8000 [Refactor] Use bindings to inject record into controllers · martynling/laravel-json-api@ca8ee29 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca8ee29

Browse files
committed
[Refactor] Use bindings to inject record into controllers
1 parent 10a9f5f commit ca8ee29

File tree

14 files changed

+425
-285
lines changed

14 files changed

+425
-285
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
},
5151
"autoload-dev": {
5252
"psr-4": {
53-
"App\\": "dummy/",
5453
"CloudCreativity\\LaravelJsonApi\\Tests\\": "tests/"
5554
}
5655
},

docs/routing.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
To define the routes available in an API, register the API in your `routes/api.php` file as follows:
66

77
```php
8-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
8+
JsonApi::api('default', ['namespace' => 'Api'], function ($api, $router) {
99
$api->resource('posts');
1010
$api->resource('comments');
1111
});
@@ -21,12 +21,12 @@ the service container instead.
2121

2222
## Controller
2323

24-
By default, routing will assume that the controller for a resource type is the resource type name suffixed with `Controller`. E.g. for a `posts` resource type, the controller will be `PostsController`. The options array to the `JsonApi::api` method takes the same options as a route group, so you can set the namespace using the `namespace` option.
24+
By default, routing will assume that the controller for a resource type is the resource type name suffixed with `Controller`. E.g. for a `posts` resource type, the controller will be `PostsController`. The options array to the `JsonApi::register` method takes the same options as a route group, so you can set the namespace using the `namespace` option.
2525

2626
If you need to override the default name for a resource type's controller, use the `controller` option as follows:
2727

2828
```php
29-
JsonApi::api('default', ['as' => 'api.', 'namespace' => 'My\Api'], function ($api, $router) {
29+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
3030
$api->resource('posts', ['controller' => 'CustomPostsController']);
3131
$api->resource('comments');
3232
});
@@ -48,7 +48,7 @@ register the following routes:
4848
To register only some of these routes, use the `only` or `except` options as follows:
4949

5050
```php
51-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
51+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
5252
$api->resource('posts', [
5353
'only' => ['index', 'read']
5454
]);
@@ -70,7 +70,7 @@ resource identifiers (and the array may be empty).
7070
Relationship routes can be registered as follows:
7171

7272
```php
73-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
73+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
7474
$api->resource('posts', [
7575
'has-one' => 'author',
7676
'has-many' => ['comments', 'likes'],
@@ -97,7 +97,7 @@ The following has-one routes are registered (using the `author` relationship on
9797
To register only some of these, use the `only` or `except` options with the relationship. E.g.
9898

9999
```php
100-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
100+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
101101
$api->resource('posts', [
102102
'has-one' => [
103103
'author' => ['only' => ['related', 'read']],
@@ -122,7 +122,7 @@ The following has-one routes are registered (using the `comments` relationship o
122122
To register only some of these, use the `only` or `except` options with the relationship. E.g.
123123

124124
```php
125-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
125+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
126126
$api->resource('posts', [
127127
'has-many' => [
128128
'comments' => ['only' => ['related', 'read'],
@@ -137,7 +137,7 @@ JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
137137
To constrain the `{resource_id}` route parameter for a specific resource, use the `id` option as follows:
138138

139139
```php
140-
JsonApi::api('default', ['as' => 'api.'], function ($api, $router) {
140+
JsonApi::register('default', ['namespace' => 'Api'], function ($api, $router) {
141141
$api->resource('posts', ['id' => '[\d]+');
142142
});
143143
```

docs/upgrade.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,13 @@ class PostsTest extends TestCase
144144

145145
protected $resourceType = 'posts';
146146

147-
protected $routePrefix = 'api-v1::';
147+
protected $api = null;
148148
}
149149
```
150150

151+
The new `$api` property specifies the API you are testing. If you do not define the property or set it to `null`,
152+
the `default` API will be used.
153+
151154
When you call the following methods, they will return an instance of our `TestResponse`, rather than `$this`. The
152155
test response is no longer assigned to a `$response` property on the test case (i.e. we made the same change as
153156
Laravel):

dummy/Http/Controllers/CommentsController.php

Lines changed: 0 additions & 114 deletions
This file was deleted.

dummy/Http/Controllers/PostsController.php

Lines changed: 0 additions & 114 deletions
This file was deleted.

src/Facades/JsonApi.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* Class Facade
2525
*
2626
* @package CloudCreativity\LaravelJsonApi
27+
* @method static void register(string $apiName, array $options, \Closure $callback)
2728
*/
2829
class JsonApi extends BaseFacade
2930
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace CloudCreativity\LaravelJsonApi\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
class SubstituteBindings
9+
{
10+
11+
/**
12+
* @param Request $request
13+
* @param Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next)
17+
{
18+
if ($record = json_api_request()->getRecord()) {
19+
$request->route()->setParameter('resource', $record);
20+
}
21+
22+
return $next($request);
23+
}
24+
}

src/Routing/ResourceGroup.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected function middleware()
8888
return array_merge($middleware, array_filter([
8989
$authorizer ? "json-api.authorize:$authorizer" : null,
9090
$validators ? "json-api.validate:$validators" : null,
91+
'json-api.bindings',
9192
]));
9293
}
9394

src/Routing/ResourceRegistrar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ResourceRegistrar
3434

3535
const KEYWORD_RELATIONSHIPS = 'relationships';
3636
const PARAM_RESOURCE_TYPE = 'resource_type';
37-
const PARAM_RESOURCE_ID = 'resource_id';
37+
const PARAM_RESOURCE_ID = 'resource';
3838
const PARAM_RELATIONSHIP_NAME = 'relationship_name';
3939

4040
/**

0 commit comments

Comments
 (0)
0