Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Latest commit

 

History

History
152 lines (105 loc) · 4.02 KB

6.x_7.x.md

File metadata and controls

152 lines (105 loc) · 4.02 KB

Upgrading To 7.x From 6.x

PHP 7.2 Required

The new minimum PHP version is now 7.2.

Updating Dependencies

Update your andrey-helldar/api-response dependency to ^7.0 in your composer.json file.

api_response function

Changed the order of api_response function attributes from:

function api_response(
    $data = null,
    int $status_code = 200,
    array $with = [],
    array $headers = [],
    bool $use_data = true
)

to:

function api_response(
    $data = null,
    int $status_code = null, // here
    array $with = [],
    array $headers = []
                            // here
)

Wrapping data

Since the goal of the package is to unify all the answers, we moved the variable definitions into a static function. So, for example, to enable or disable wrapping content in the data key, you need to call the wrapped or withoutWrap method:

use Helldar\ApiResponse\Services\Response;

Response::wrapped();
// or
Response::withoutWrap();

If you use the Laravel or Lumen framework, then in the App\Providers\AppServiceProvider file you need to add the following lines:

use Helldar\ApiResponse\Services\Response;

public function register()
{
    $this->registerExtraData();
}

protected function registerExtraData(): void
{
    config('app.debug')
        ? Response::allowWith()
        : Response::withoutWith();
}

Extra data

In some cases, when returning answers, you must also give additional data. Such as stack trace, for example.

To prevent this data from getting in response to production, you can globally set a label to show or hide this data:

use Helldar\ApiResponse\Services\Response;

env('APP_DEBUG')
    ? Response::allowWith()
    : Response::withoutWith();

For example, if you use the Laravel or Lumen framework, then in the App\Providers\AppServiceProvider file you need to add the following lines:

use Helldar\ApiResponse\Services\Response;

public function register()
{
    $this->registerExtraData();
}

protected function registerExtraData(): void
{
    config('app.debug')
        ? Response::allowWith()
        : Response::withoutWith();
}

Helldar\ApiResponse\Services\Response

An interface with public methods is now available:

namespace Helldar\ApiResponse\Contracts;

use Symfony\Component\HttpFoundation\JsonResponse;

interface Responsable
{
    public static function allowWith(): void;

    public static function withoutWith(): void;

    public static function wrapped(): void;

    public static function withoutWrap(): void;

    public function with(array $with = []): self;

    public function headers(array $headers = []): self;

    public function statusCode(int $code = null): self;

    public function data($data = null): self;

    public function response(): JsonResponse;
}

The data method

data method from Helldar\ApiResponse\Services\Response removed many attributes:

function data($data = null) {}

Laravel & Lumen Frameworks

If you are using Laravel or Lumen framework, update the dependency in the App\Exceptions\Handler file depending on your application version and needs:

Replace use Helldar\ApiResponse\Support\LaravelException as ExceptionHandler; with:

Version \ Type API + WEB Only API
8.x Helldar\ApiResponse\Exceptions\Laravel\Eight\Handler as ExceptionHandler Helldar\ApiResponse\Exceptions\Laravel\Eight\ApiHandler as ExceptionHandler
7.x Helldar\ApiResponse\Exceptions\Laravel\Seven\Handler as ExceptionHandler Helldar\ApiResponse\Exceptions\Laravel\Seven\ApiHandler as ExceptionHandler

Due to the fact that since Laravel 7.x the typing in methods has been changed from Exception to Throwable, we will only support the last two versions of the framework.

BUT this does not prevent you from using the api_response() helper yourself anywhere in your application, including your Handler file. Our file is just a ready-made example for easy implementation, and no one bothers you to make your own using its analogue.