multividas / api-responser
Composer package to facilitates the process of structuring and generating API responses
Installs: 535
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Type:package
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: dev-master
- multividas/query-filters: dev-main
- orchestra/testbench: 8.x-dev
- phpunit/phpunit: 9.6.x-dev
- squizlabs/php_codesniffer: 4.0.x-dev
This package is auto-updated.
Last update: 2024-11-13 17:15:23 UTC
README
API Responser
Composer package to facilitates the process of structuring and generating API responses
Installation
Require this package with composer.
composer require multividas/api-responser
ServiceProvider:
[Optional] Adding the ApiResponserServiceProvider to the providers array in config/app.php
\Multividas\ApiResponser\Providers\ApiResponserServiceProvider::class,
[Optional] To get X-Application-Name http response header, Copy the package config to your local config with the publish command:
php artisan vendor:publish --tag=api-responser-config
Usage
use \Multividas\ApiResponser\Traits\ApiResponser; class Controller extends BaseController { use ApiResponser; }
ApiResponser Interface
Using the ApiResponser
interface methods.
showAll()
method receivesCollection|JsonResource
as its param.showOne()
method receivesModel|JsonResource $instance
as its param.
use \Multividas\ApiResponser\Interfaces\ApiRepositoryInterface; class PostsController extends Controller { public function __construct( public ApiRepositoryInterface $apiRepository ) { } public function index(): JsonResponse { return $this->apiRepository->showAll(Post::all()); } public function show(Post $post): JsonResponse { if (!$post instanceof Post) { return $this->infoResponse('Item Not Found', 404, []); } return $this->apiRepository->showOne($post); } }
ApiResponser Facade
Using the ApiResponser
facade design pattern.
use Multividas\ApiResponser\Facades\ApiResponser; class PostsController extends Controller { public function index(): JsonResponse { return ApiResponser::showAll(Post::all()); } public function show(string $postId): JsonResponse { $post = Post::find($postId); if (!$post instanceof Post) { return $this->infoResponse('Post Not Found', 404, (object)[]); } return ApiResponser::showOne($post); } }
This approach provides a cleaner and more organized way to interact with the ApiRepositoryInterface
instance in your controller methods.
Success Response
Successful response containing the requested data and an appropriate status code.
{ "data": [], "code": 200, "meta": {} }
Learn more: Multividas API Responser
Run PHPUnit tests
composer test
🤝 Contributing
Please read the contributing guide.
🛡️ Security Issues
If you discover a security vulnerability within Multividas, we would appreciate your help in disclosing it to us responsibly, please check out our security issues guidelines.
🛡️ License
Licensed under the MIT license.
Email: multividasdotcom@gmail.com