2017 © Pedro Peláez
 

yii2-extension yii2-rest

The Yii2 Rest extension for the Yii framework

image

leegoway/yii2-rest

The Yii2 Rest extension for the Yii framework

  • Tuesday, November 8, 2016
  • by leegoway
  • Repository
  • 1 Watchers
  • 1 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

The Yii2 Rest extension for the Yii framework

The Yii2 Rest extension for the Yii framework, (*1)

Installation

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)

Usage

格式化的ActiveController输出

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)

a、modify your config file
'errorHandler' => [
    'class' => 'leegoway\rest\RestErrorHandler',
    'errorAction' => 'site/error',
    'restErrorAction' => 'error/error'
],
b、modify your error/error controller
<?php
namespace app\controllers;

class MyController extends leegoway\rest\RestErrorController
{
}
c、throw exception which based on leegoway\rest\RestException
throw new leegoway\rest\RestException('msg', code);

对actions进行权限控制

Third, if you need check permission of action level, you cound write code like following:, (*11)

a、and you should reuqire leegoway\yii2-uic
composer require leegoway\yii2-uic
b、modify your config
return [
    'components' => [
        'uicAuther' => [
            'class' => 'leegoway\uic\Auther',
            'domain' => 'autops.corp.elong.com',//cookie的domain属性
            'path' => '/',//cookie的路径
            'expire' => 7200 //超时时间
        ]
    ],
];
c、check your action permission
class MyController extends RestActiveController
{
    public $uicRules = [
        'index' => [
            'orgainzationId' => '...',
            'permissionId' => '...'
        ],
        'view' =>  [
            'orgainzationId' => '...',
            'permissionId' => '...'
        ],
        ...
    ];

}

The Versions

08/11 2016

dev-master

9999999-dev

The Yii2 Rest extension for the Yii framework

  Sources   Download

BSD 3-Clause

The Requires

 

by Avatar leegoway

rest yii2 outputformat