dev-master
9999999-devThe Yii2 Rest extension for the Yii framework
BSD 3-Clause
The Requires
by leegoway
rest yii2 outputformat
The Yii2 Rest extension for the Yii framework
The Yii2 Rest extension for the Yii framework, (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist leegoway/yii2-rest "*"
or add, (*4)
"leegoway/yii2-rest": "*"
to the require section of your composer.json
file., (*5)
Once the extension is installed, simply use it in your controller as base controller by :, (*6)
then you can get formatted output like : {"code":200, "msg":"", "data":{...}}, (*7)
Besides, use the formatter class anywhere you need like this :, (*8)
use leegoway\rest\Formatter; ... Formatter::success($data);//返回业务数据 Formatter::error($errors);//返回校验失败的数据 Formatter::fail($msg);//业务逻辑问题导致的失败
then you can get formatted output., (*9)
Last,you can use following method to catch your defined exception and output formatted response., (*10)
'errorHandler' => [ 'class' => 'leegoway\rest\RestErrorHandler', 'errorAction' => 'site/error', 'restErrorAction' => 'error/error' ],
error/error
controller<?php namespace app\controllers; class MyController extends leegoway\rest\RestErrorController { }
leegoway\rest\RestException
throw new leegoway\rest\RestException('msg', code);
Third, if you need check permission of action level, you cound write code like following:, (*11)
composer require leegoway\yii2-uic
return [ 'components' => [ 'uicAuther' => [ 'class' => 'leegoway\uic\Auther', 'domain' => 'autops.corp.elong.com',//cookie的domain属性 'path' => '/',//cookie的路径 'expire' => 7200 //超时时间 ] ], ];
class MyController extends RestActiveController { public $uicRules = [ 'index' => [ 'orgainzationId' => '...', 'permissionId' => '...' ], 'view' => [ 'orgainzationId' => '...', 'permissionId' => '...' ], ... ]; }
The Yii2 Rest extension for the Yii framework
BSD 3-Clause
rest yii2 outputformat