razonyang/yii2-jsend JSend port for Yii2

JSend port for Yii2

  1. Installation
  2. Usage
  3. Error Handler

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads LICENSE

The package is a Yii2's implementation of JSend specification.

Installation

composer require razonyang/yii2-jsend

Usage

Update resposne formater and error handler:

return [
    'components' => [
        'response' => [
            'formatters' => [
                \yii\web\Response::JSON => [
                    'class' => \RazonYang\Yii2\JSend\Formatter::class,
                ],
            ],
        ],
        'errorHandler' => [
            'class' => \RazonYang\Yii2\JSend\ErrorHandler::class,
        ],
    ],
];

Change you rest controller serializer:

public $serializer = \RazonYang\Yii2\JSend\Serializer::class;

Error Handler

ErrorHandler converts exception to error payload:

{
    "status": "error",
    "code": "exception code",
    "message": "exception message"
}

It can also returns the optional data field by throwing a JSend's Exception:

throws new RazonYang\Jsend\Exception($message, $code, $data, $previous);

// you can also define your own exception:
class MyException extends RazonYang\Jsend\Exception
{
}

throws new MyException();
{
    "status": "error",
    "code": "exception code",
    "message": "exception message",
    "data": "exception data"
}
1 0
1 follower
4 187 downloads
Yii Version: 2.0
License: BSD-3-Clause
Category: Others
Developed by: Razon Yang
Created on: Aug 6, 2019
Last updated: (not set)
Packagist Profile
Github Repository

Related Extensions