Skip to content
This repository was archived by the owner on Feb 18, 2023. It is now read-only.

Commit c0f5427

Browse files
committed
re load dingo and responses with tests in Green :)
1 parent b69a7db commit c0f5427

File tree

16 files changed

+919
-477
lines changed

16 files changed

+919
-477
lines changed

app/Exceptions/Handler.php

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ public function report(Exception $exception)
5252
*/
5353
public function render($request, Exception $exception)
5454
{
55-
if($exception instanceof NotFoundHttpException){
56-
return $this->renderNotFoundException($exception);
57-
}
58-
if($exception instanceof ModelNotFoundException) {
59-
return $this->renderModelNotFoundException($exception);
60-
}
61-
if($exception instanceof AccessDeniedHttpException) {
62-
return $this->renderAccessDeniedException($exception);
63-
}
6455
return parent::render($request, $exception);
6556
}
6657

@@ -73,72 +64,11 @@ public function render($request, Exception $exception)
7364
*/
7465
protected function unauthenticated($request, AuthenticationException $exception)
7566
{
67+
7668
if ($request->expectsJson()) {
7769
return response()->json(['message' => 'Unauthenticated.'], 401);
7870
}
7971

8072
return redirect()->guest('login');
8173
}
82-
83-
/**
84-
* Create a response object from the given validation exception.
85-
*
86-
* @param \Illuminate\Validation\ValidationException $e
87-
* @param \Illuminate\Http\Request $request
88-
* @return \Symfony\Component\HttpFoundation\Response
89-
*/
90-
protected function convertValidationExceptionToResponse(ValidationException $e, $request)
91-
{
92-
if ($e->response) {
93-
return $e->response;
94-
}
95-
96-
$errors = $e->validator->errors()->getMessages();
97-
98-
if ($request->expectsJson()) {
99-
return response()->json([
100-
'message' => 'Validation error',
101-
'errors' => $errors
102-
], 422);
103-
}
104-
105-
return redirect()->back()->withInput(
106-
$request->input()
107-
)->withErrors($errors);
108-
}
109-
110-
/**
111-
* @return \Illuminate\Http\JsonResponse
112-
*/
113-
protected function renderNotFoundException($e)
114-
{
115-
if(request()->expectsJson()){
116-
return response()->json(['message' => 'Not Found'], 404);
117-
}
118-
return $this->renderHttpException($e);
119-
}
120-
121-
/**
122-
* @param $e
123-
* @return \Illuminate\Http\JsonResponse
124-
*/
125-
protected function renderModelNotFoundException($e)
126-
{
127-
if(request()->expectsJson()){
128-
return response()->json(['message' => 'Not Found'], 404);
129-
}
130-
throw new NotFoundHttpException();
131-
}
132-
133-
/**
134-
* @param $e
135-
* @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response
136-
*/
137-
protected function renderAccessDeniedException($e)
138-
{
139-
if(request()->expectsJson()){
140-
return response()->json(['message' => 'Forbidden'], 403);
141-
}
142-
return $this->renderHttpException($e);
143-
}
14474
}

app/Http/Controllers/Api/PingController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers\Api;
44

5+
use Dingo\Api\Routing\Helpers;
56
use App\Http\Controllers\Controller;
67

78
/**
@@ -10,12 +11,14 @@
1011
*/
1112
class PingController extends Controller
1213
{
14+
15+
use Helpers;
1316
/**
1417
* @return \Illuminate\Http\JsonResponse
1518
*/
1619
public function index()
1720
{
18-
return response()->json([
21+
return $this->response->array([
1922
'status' => 'ok',
2023
'timestamp' => \Carbon\Carbon::now()
2124
]);

app/Http/Controllers/Api/Users/UsersController.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
use App\Entities\User;
66
use Illuminate\Http\Request;
7+
use Dingo\Api\Routing\Helpers;
78
use App\Http\Controllers\Controller;
89
use App\Transformers\Users\UserTransformer;
9-
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
1010

1111
/**
1212
* Class UsersController
@@ -15,6 +15,8 @@
1515
class UsersController extends Controller
1616
{
1717

18+
use Helpers;
19+
1820
/**
1921
* @var User
2022
*/
@@ -43,9 +45,7 @@ public function index(Request $request)
4345
{
4446
$paginator = $this->model->with('roles.permissions')
4547
->paginate($request->get('limit', config('app.pagination_limit')));
46-
return fractal()->collection($paginator->getCollection(), new UserTransformer())
47-
->paginateWith(new IlluminatePaginatorAdapter($paginator))
48-
->respond();
48+
return $this->response->paginator($paginator, new UserTransformer());
4949
}
5050

5151

@@ -56,7 +56,7 @@ public function index(Request $request)
5656
public function show($id)
5757
{
5858
$user = $this->model->with('roles.permissions')->byUuid($id)->firstOrFail();
59-
return fractal($user, new UserTransformer())->respond();
59+
return $this->response->item($user, new UserTransformer());
6060
}
6161

6262

@@ -72,9 +72,7 @@ public function store(Request $request)
7272
'password' => 'required|min:8|confirmed'
7373
]);
7474
$user = $this->model->create($request->all());
75-
return fractal($user, new UserTransformer())
76-
->respond(201)
77-
->header('location', url('api/users/'.$user->uuid));
75+
return $this->response->created(url('api/users/'.$user->uuid));
7876
}
7977

8078

@@ -98,7 +96,7 @@ public function update(Request $request, $uuid)
9896
}
9997
$this->validate($request, $rules);
10098
$user->update($request->except('_token'));
101-
return fractal($user->fresh(), new UserTransformer())->respond();
99+
return $this->response->item($user, new UserTransformer());
102100
}
103101

104102
/**
@@ -110,6 +108,6 @@ public function destroy(Request $request, $uuid)
110108
{
111109
$user = $this->model->byUuid($uuid)->firstOrFail();
112110
$user->delete();
113-
return response(null, 204);
111+
return $this->response->noContent();
114112
}
115113
}

app/Http/Controllers/Controller.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6+
use Dingo\Api\Exception\ResourceException;
67
use Illuminate\Foundation\Bus\DispatchesJobs;
78
use Illuminate\Routing\Controller as BaseController;
89
use Illuminate\Foundation\Validation\ValidatesRequests;
@@ -22,10 +23,7 @@ class Controller extends BaseController
2223
protected function buildFailedValidationResponse(Request $request, array $errors)
2324
{
2425
if ($request->expectsJson()) {
25-
return response([
26-
'message' => 'Validation Error',
27-
'errors' => $errors
28-
], 422);
26+
throw new ResourceException("Validation Error", $errors);
2927
}
3028

3129
return redirect()->to($this->getRedirectUrl())

app/Http/Kernel.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ class Kernel extends HttpKernel
3636
\Illuminate\Routing\Middleware\SubstituteBindings::class,
3737
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
3838
],
39-
40-
'api' => [
41-
'throttle:60,1',
42-
'bindings',
43-
],
4439
];
4540

4641
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Dingo\Api\Routing\Helpers;
6+
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Auth\AuthenticationException;
8+
use Illuminate\Database\Eloquent\ModelNotFoundException;
9+
10+
class ErrorHandlerServiceProvider extends ServiceProvider
11+
{
12+
13+
use Helpers;
14+
15+
/**
16+
* Bootstrap the application services.
17+
*
18+
* @return void
19+
*/
20+
public function boot()
21+
{
22+
//
23+
}
24+
25+
/**
26+
* Register the application error handlers.
27+
*
28+
* @return void
29+
*/
30+
public function register()
31+
{
32+
// register the error handler for the authentication exception.
33+
app('Dingo\Api\Exception\Handler')->register(function (AuthenticationException $exception) {
34+
return $this->response->errorUnauthorized('Unauthenticated.');
35+
});
36+
app('Dingo\Api\Exception\Handler')->register(function (ModelNotFoundException $exception) {
37+
return $this->response->errorNotFound('404 Not Found');
38+
});
39+
}
40+
}

app/Providers/RouteServiceProvider.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,14 @@ protected function mapWebRoutes()
5858

5959
/**
6060
* Define the "api" routes for the application.
61+
* Since we are using Dingo, the web middleware group will not apply
6162
*
6263
* These routes are typically stateless.
6364
*
6465
* @return void
6566
*/
6667
protected function mapApiRoutes()
6768
{
68-
Route::prefix('api')
69-
->middleware('api')
70-
->namespace($this->namespace)
71-
->group(base_path('routes/api.php'));
69+
require base_path('routes/api.php');
7270
}
7371
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "joselfonseca/laravel-api",
33
"description": "Laravel API starter Kit will give you most of the boilerplate that you need for creating Laravel API's",
4-
"keywords": ["framework", "laravel", "Api", "REST", "oAuth2"],
4+
"keywords": ["framework", "laravel", "Api", "REST", "oAuth2", "Dingo API"],
55
"license": "MIT",
66
"type": "project",
77
"authors": [
@@ -19,7 +19,7 @@
1919
"joselfonseca/laravel-tactician": "0.3.*",
2020
"laravel/tinker": "~1.0",
2121
"predis/predis": "^1.1",
22-
"spatie/laravel-fractal": "^3.3",
22+
"dingo/api": "1.0.*@dev",
2323
"spatie/laravel-permission": "^1.7",
2424
"webpatser/laravel-uuid": "^2.0"
2525
},

0 commit comments

Comments
 (0)