Skip to content

Commit e91159e

Browse files
authored
Feature/policy collections update (#19)
* Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce
1 parent e77a6e9 commit e91159e

13 files changed

+278
-203
lines changed

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ You can override the methods by simply putting in your own methods to override -
4242
* PUT (class::update) - triggers a new `Phpsa\LaravelApiController\Events\Updated` Event which has the updated record available as `$record`
4343
* DELETE (class::destry) - triggers a new `Phpsa\LaravelApiController\Events\Deleted` Event which has the deleted record available as `$record`
4444

45+
## Policies
46+
47+
Policies: https://laravel.com/docs/6.x/authorization#generating-policies
48+
49+
Generate with `php artisan make:policy PostPolicy --model=Post`
50+
51+
* Get list - calls the `viewAny` policy
52+
* Get single - calls the `view` policy
53+
* Post New - calls the `create` policy
54+
* Put Update - calls the `update` policy
55+
* Delete item - calls the `delete` policy
56+
57+
## Resources / Collections (Transforming)
58+
Resources: https://laravel.com/docs/6.x/eloquent-resources
59+
60+
Generate with
61+
`php artisan make:resource UserResource` and `php artisan make:resource UserCollection`
62+
63+
in your controller override the following params:
64+
```php
65+
protected $resourceSingle = UserResource::class;
66+
protected $resourceCollection = UserCollection::class;
67+
```
4568

4669
## Filtering
4770

@@ -106,13 +129,16 @@ see https://laravel.com/docs/5.8/validation#conditionally-adding-rules
106129

107130
The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:
108131

109-
* `protected $resourceKeySingular = 'data';` Resource key for an item.
110-
* `protected $resourceKeyPlural = 'data';` Resource key for a collection.
111-
* `protected $defaultFields = ['*'];` Default Fields to respond with
112-
* `protected $defaultSort = null;` Set the default sorting for queries.
113-
* `protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
114-
* `protected $maximumLimit = 0;` Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
115-
* `protected $unguard = false;` Do we need to unguard the model before create/update?
132+
* **DEPRECATED** `protected $resourceKeySingular = 'data';`
133+
* **DEPRECATED** `protected $resourceKeyPlural = 'data';`
134+
135+
* `protected $resourceSingle = JsonResource::class;` Collection to use for your single resource
136+
* `protected $resourceCollection = ResourceCollection::class;` Collection to use for your resource collection
137+
* `protected $defaultFields = ['*'];` Default Fields to respond with
138+
* `protected $defaultSort = null;` Set the default sorting for queries.
139+
* `protected $defaultLimit = 25;` Number of items displayed at once if not specified. (0 = maximumLimit)
140+
* `protected $maximumLimit = 0;` Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.
141+
* `protected $unguard = false;` Do we need to unguard the model before create/update?
116142

117143
## Security
118144

@@ -122,7 +148,11 @@ instead of using the issue tracker.
122148
## Credits
123149

124150
- [Craig G Smith](https://github.com/phpsa)
151+
- [Phil Taylor]()
125152
- [All contributors](https://github.com/phpsa/laravel-api-controller/graphs/contributors)
126153

127-
[badge_laravel]: https://img.shields.io/badge/Laravel-5.8%20to%205.8-orange.svg?style=flat-square
154+
## Sponsors
155+
- [Custom D](https://customd.com)
156+
157+
[badge_laravel]: https://img.shields.io/badge/Laravel-5.8%20to%206-orange.svg?style=flat-square
128158
[badge_issues]: https://img.shields.io/github/issues/ARCANEDEV/Support.svg?style=flat-square

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"require": {
1818
"php": ">=7.1.3",
19-
"illuminate/support": "~5.8.0|~6.0|~6.1"
19+
"illuminate/support": "~5.8.0|^6.0"
2020
},
2121
"require-dev": {
2222
"orchestra/testbench": "~3.8.0",

config/laravel-api-controller.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
<?php
22

33
return [
4-
/*
5-
* Relative path from the app directory to api controllers directory.
6-
*/
7-
'controllers_dir' => 'Http/Controllers/Api',
8-
/*
9-
* Relative path from the app directory to the api routes file.
10-
*/
11-
'routes_file' => '../routes/api.php',
12-
/*
13-
* Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
14-
*/
15-
'models_base_dir' => 'Models',
16-
/*
17-
* Relative path from the base directory to the api controller stub.
18-
*/
19-
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
20-
/*
21-
* Relative path from the base directory to the route stub.
22-
*/
23-
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
4+
// Relative path from the app directory to api controllers directory.
5+
'controllers_dir' => 'Http/Controllers/Api',
6+
// Relative path from the app directory to the api routes file.
7+
'routes_file' => '../routes/api.php',
8+
// Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
9+
'models_base_dir' => 'Models',
10+
// Relative path from the base directory to the api controller stub.
11+
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
12+
// Relative path from the base directory to the route stub.
13+
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
2414

2515
'parameters' => [
2616
'include' => 'include',
@@ -30,5 +20,4 @@
3020
'page' => 'page',
3121
'group' => 'group',
3222
],
33-
3423
];

src/Events/Created.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Created
2727
*
2828
* @param \Illuminate\Database\Eloquent\Model $record
2929
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
30-
*
31-
* @return void
3230
*/
3331
public function __construct($record, $request)
3432
{

src/Events/Deleted.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Deleted
2727
*
2828
* @param \Illuminate\Database\Eloquent\Model $record
2929
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
30-
*
31-
* @return void
3230
*/
3331
public function __construct($record, $request)
3432
{

src/Events/Updated.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class Updated
2727
*
2828
* @param \Illuminate\Database\Eloquent\Model $record
2929
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
30-
*
31-
* @return void
3230
*/
3331
public function __construct($record, $request)
3432
{

src/Generator/ApiMakeCommand.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,38 @@
1111
class ApiMakeCommand extends Command
1212
{
1313
use DetectsApplicationNamespace;
14+
1415
/**
1516
* The filesystem instance.
1617
*
1718
* @var \Illuminate\Filesystem\Filesystem
1819
*/
1920
protected $files;
21+
2022
/**
2123
* The console command name.
2224
*
2325
* @var string
2426
*/
2527
protected $name = 'make:api';
28+
2629
/**
2730
* The console command description.
2831
*
2932
* @var string
3033
*/
3134
protected $description = 'Create api controller and api routes for a given model (phpsa/laravel-api-controller)';
35+
3236
/**
3337
* The array of variables available in stubs.
3438
*
3539
* @var array
3640
*/
3741
protected $stubVariables = [
38-
'app' => [],
39-
'model' => [],
40-
'controller' => [],
41-
'route' => [],
42+
'app' => [],
43+
'model' => [],
44+
'controller' => [],
45+
'route' => [],
4246
];
4347

4448
protected $modelsBaseNamespace;
@@ -56,8 +60,6 @@ public function __construct(Filesystem $files)
5660

5761
/**
5862
* Execute the console command.
59-
*
60-
* @return void
6163
*/
6264
public function handle()
6365
{
@@ -169,6 +171,7 @@ protected function addRoutes()
169171
$routesFile = app_path(config('laravel-api-controller.routes_file'));
170172
// read file
171173
$lines = file($routesFile);
174+
172175
if (! $lines) {
173176
//@todo - better error handling here
174177
return false;
@@ -183,6 +186,7 @@ protected function addRoutes()
183186
}
184187
// save file
185188
$fp = fopen($routesFile, 'w');
189+
186190
if (! is_resource($fp)) {
187191
//@todo - better error handling here
188192
return false;
@@ -202,6 +206,7 @@ protected function addRoutes()
202206
protected function createClass($type)
203207
{
204208
$path = $this->getPath($this->stubVariables[$type]['fullNameWithoutRoot']);
209+
205210
if ($this->files->exists($path)) {
206211
$this->error(ucfirst($type).' already exists!');
207212

src/Generator/stubs/controller.stub

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class {{controller.name}} extends Controller
2626
* @return Response
2727
*/
2828
public function index(Request $request)
29-
{
30-
return $this->handleIndexAction($request);
31-
}
29+
{
30+
return $this->handleIndexAction($request);
31+
}
3232

3333
/**
3434
* Store a newly created resource in storage.
@@ -54,7 +54,7 @@ class {{controller.name}} extends Controller
5454
*/
5555
public function show($id, Request $request)
5656
{
57-
return $this->handleShowAction($id, $request);
57+
return $this->handleShowAction($id, $request);
5858
}
5959

6060
/**

0 commit comments

Comments
 (0)