Skip to content

soulaimaneyahya/api-responser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Query Option package logo

API Responser

Tests Total Downloads License

Composer package to facilitates the process of structuring and generating API responses

Installation

Require this package with composer. It is recommended to only require the package for development.

composer require multividas/api-responser --dev

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;
}

Dependency Injection

PostsController has __construct() method initializes a property apiRepository with an instance of the ApiRepositoryInterface.

showAll() method receives Collection|JsonResource as its param.

showOne() method receives Model|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);
    }
}

Facades

Using the ApiResponser to access the methods of ApiRepositoryInterface in your PostsController.

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.


Run PHPUnit tests

composer test

Need helps? Reach us

Multividas.com Ⓜ️

Email: contact@multividas.com

🌌 🚀

About

composer package to facilitates the process of structuring and generating API responses

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • PHP 100.0%