Link to the repository
[Only for premium members]
[Only for premium members]
In this lesson, we will discuss SOLID code with the Single Responsibility Principle and clearer method names.
First, the project has a "Base" API Controller with a few methods that can be reused in other Controllers. Some of them are for typical API responses.
app/Http/Controllers/Api/V1/ApiController.php
class ApiController extends Controller // ... public function notAuthorized(string $message = 'You are not authorized.') { return $this->error($message, Response::HTTP_UNAUTHORIZED); } public function notFound(string $message = 'Not Found.') { return $this->error($message, Response::HTTP_NOT_FOUND); } public function unexpectedError(string $message = 'An unexpected error occurred.') { return $this->error($message, Response::HTTP_INTERNAL_SERVER_ERROR); } public function dbError(string $message = 'Database error.') { return $this->error($message, Response::HTTP_INTERNAL_SERVER_ERROR); }
But wait, what is that $this->error()
?
Then I noticed a trait for API Responses:
app/Http/Controllers/Api/V1/ApiController.php
use App\Traits\V1\ApiResponses; class ApiController extends Controller{ use ApiResponses;
Inside that Trait, we have more "general" methods for API responses:
app/Traits/V1/ApiResponses.php:
trait ApiResponses{ protected function ok(string $message, array $data = []) { return $this->success($message, $data, 200); } protected function success(string $message, array $data = [], int $statusCode = 200) { return response()->json([ 'data' => $data, 'message' => $message, 'status' => $statusCode, ], $statusCode); } protected function error($message, $statusCode) { return response()->json(['errors' => $message, 'status' => $statusCode], $statusCode); }}
And I had a few questions here:
So, I decided to...