JSend port for Yii2 ¶
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"
}
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.