Skip to content
JSend formatter, serializer and error handler for Yii2
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Convert non UserException or HttpException to ServerErrorHttpExceptio… Aug 21, 2019
tests
.gitignore
.scrutinizer.yml
.styleci.yml
.travis.yml First commit Aug 21, 2019
CHANGELOG.md First commit Aug 21, 2019
LICENSE First commit Aug 21, 2019
README.md
composer.json
phpunit.xml.dist

README.md

JSend port for Yii2

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"
}
You can’t perform that action at this time.