youngmayor/app-response

A laravel package for providing unified easy to use response to our laravel applicatons

1.1.0 2022-07-12 20:48 UTC

This package is auto-updated.

Last update: 2024-10-13 01:37:25 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

This is a simple laravel package for providing a unified response for our application.

Installation

You can install the package via composer:

composer require youngmayor/app-response

Usage

The package uses auto discovery hence if you use Laravel 5.5 or above, installing the package automatically registers it in your application.

However, if you use Laravel 5.4 or below you will need to add the below snipet to your config/app.php to register the Service Provider and alias

'providers' => [
    // ...
    YoungMayor\AppResponse\AppResponseServiceProvider,
    // ...
],

'aliases' => [
    // ...
    "AppResponse": "YoungMayor\AppResponse\Facade\AppResponse"
    // ...
],

The package can now be added to our controller actions using one of the methdos below

  1. Injecting the API response helper
// ...
use YoungMayor\AppResponse\API; 
// ...

class SampleController extends Controller
{
    public function sampleMethod(API $api)
    {
        // ...
        return $api->success("Action was successful");
    }
}
  1. Injecting the Web response helper
// ...
use YoungMayor\AppResponse\Web; 
// ...

class SampleController extends Controller
{
    public function sampleMethod(Web $web)
    {
        // ...
        return $web->success("Action was successful");
    }
}
  1. Injecting the automatic response helper
// ...
use YoungMayor\AppResponse\Auto; 
// ...

class SampleController extends Controller
{
    public function sampleMethod(Auto $auto)
    {
        // ...
        return $auto->success("Action was successful");
    }
}
  1. Using the package facade
// ...
use YoungMayor\AppResponse\Facade\AppResponse; 
// or 
use AppReponse; // this utilises the Facade aliase
// ...

class SampleController extends Controller
{
    public function sampleMethod()
    {
        // ... 
        return AppResponse::success("Action was successful"); 
    }
}
  1. Using the helper function
class SampleController extends Controller
{
    public function sampleMethod()
    {
        // ...
        return appResponse()->success("Action was successful"); // for automatic response
        // or 
        return appResponse()->api->success("Action was successful"); // for API only resppnse
        // or 
        return appResponse()->web->success("Action was successful"); // for Web only resppnse
    }
}

Response Types

The package provides two forms of responses, Web and API. It also provides an Auto responder which essentially responds as API if Accept: application/json header exists and responds as Web if the header is missing.

API Response

This returns a JSON response

Example

{
    "status": "success", 
    "statusCode": 200, 
    "message": "Action was successful", 
    "data": {
        "name": "Foo Bar", 
        "email": "foobar@example.com"
    }
}

Web Response

This renders a web page as the response. An example is attached below Screenshot

Web response can be completed turned off by making setting the app-response.render-web-as-view configuration to false. The configuration file can be published using the below command

php artisan vendor:publish --provider="YoungMayor\AppResponse\AppResponseServiceProvider" --tag="config"

The web response can also be customised. To publish the views run the below code on the root of your application terminal

php artisan vendor:publish --provider="YoungMayor\AppResponse\AppResponseServiceProvider" 

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email youngmayor.dev@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.