ilyatos / laravel-api-response
Give it to me as JSON!
dev-master
2021-01-23 18:44 UTC
Requires
- php: ^7.3
- illuminate/http: ~5.0|~6.0|~7.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- psy/psysh: @stable
This package is auto-updated.
Last update: 2024-04-29 23:39:45 UTC
README
Welcome to simple wrapper of JsonResponse!
Requirements
- Laravel 5/6/7
You don't need to register the service provider manually.
Usage example
All you need to do is inject the \Ilyatos\ApiResponse\Contracts\Response
interface into your controller/middleware/etc. constructor.
Just look at this nice example and everything will be clear for you:
<?php declare(strict_types=1); namespace App\Http\Controllers; use Illuminate\Http\JsonResponse; use Illuminate\Routing\Controller as BaseController; use Ilyatos\ApiResponse\Contracts\Response; class Controller extends BaseController { /** * @var Response */ protected $response; public function __construct(Response $response) { $this->response = $response; } public function example(): JsonResponse { return $this->response->withMessage('hello!'); } public function anotherExample(): JsonResponse { return $this->response->withDefaultMessage(\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND); } public function yetAnotherExample(): JsonResponse { return $this->response->withData(['status' => 'nice']); } }