This package is abandoned and no longer maintained. The author suggests using the yii-event-handlers/handlers package instead.

handlers, base yii to use event and handlers

V1.0.6 2020-05-25 02:16 UTC

This package is not auto-updated.

Last update: 2021-05-20 07:00:28 UTC


README

介绍

   该package基于yii的事件,构建action这步的操作流程,引导开发者使用yii的事件。开发者可以把注意力放在事件处理器的对象
化开发,一定程度上对代码进行了整理和规范化,避免因为严重的面向过程开发带来代码的低可读性问题。
   开发者根据实际业务或功能需要,把代码脱耦成多个功能相对独立的事件处理器,并对处理器进行对象化以提高代码的阅读性。每个
事件处理器可对整个事件的数组变量$event->$event_var进行设置,达到事件处理器之间的数据耦合。

软件架构

基于yii2的事件。

安装教程

  1. 安装:composer require yii-event-handlers/handlers
  2. 更新:composer update yii-event-handlers/handlers

使用说明

  1. 使用示例:
    use handlers\controllers\Controller;
    class TestController extends Controller
    {
    public function actionIndex()
    {
        $this->handlers = array(
            'app\...\Handlers'=>'test',
        );
        $this->handle();
    }
    }
    

use handlers\events\Event; class Handlers {

/**
 * @param Event $event
 */
test($event){
    //你的逻辑代码
}

}

2.  事件Event可读属性:

$event->requestArgs;//array类型,接收到的参数 $event->requestTime;//string类型,进入action的时间戳 $event->hostInfo;//string类型,请求域名

3.  事件Event可读可写属性:

$event->returnData;//任意类型,整个action的返回数据 $event->eventVar;//任意类型,事件变量,可通过该变量在事件处理器间传递数据 $event->debug;//boolean类型,自定义debug标记 $event->errorCode;//int类型,整个事件的异常码,0为事件正常,其他为异常码。设置为正数,返回的时候对应为负数 $event->errorMsg;//string类型,整个事件的异常信息

4.  事件Event可使用方法:

(1)当Controller指定了多个handlers时,有时候根据逻辑需要,在某个事件处理器处理完后,将不再处理后面的事件处理器,可使用 以下事件方法中断返回:

$event->stop();

如果想在当前处理器未处理完就正常返回,可通过return 提前正常返回:

return $event->stop();

(2)如果处理某个处理器的时候,遇到致命错误,或自定义致命错误,以使整个action返回错误,可通过以下代码处理:

$event->error_code = 100;
$event->error_msg = 100;
return $event->error();

注意一定要return,否则当前事件处理器将继续执行完。

5.  handle()的使用:

(1)多个事件处理器: use handlers\controllers\Controller; class TestController extends Controller {

public function actionIndex()
{
    $this->handlers = array(
        'app\...\Handlers'=>'test',
        ['app\...\Handlers'=>'test1'],//绑定同一个类下的多个事件处理器的时候,用[]避免key重复
        'app\...\Handlers1'=>'test',//可指定多个类下的事件处理器
    );
    $this->handle();
}

} (2)get请求(默认为post请求): use handlers\controllers\Controller; class TestController extends Controller {

public function actionIndex()
{
    $this->handlers = array(
        'app\...\Handlers'=>'test',
        ['app\...\Handlers'=>'test1'],//绑定同一个类下的多个事件处理器的时候,用[]避免key重复
        'app\...\Handlers1'=>'test',//可指定多个类下的事件处理器
    );
    $this->handle([],'get');//指定get请求
}

} (3)不使用事务提交: use handlers\controllers\Controller; class TestController extends Controller {

public function actionIndex()
{
    $this->handlers = array(
        'app\...\Handlers'=>'test',
        ['app\...\Handlers'=>'test1'],//绑定同一个类下的多个事件处理器的时候,用[]避免key重复
        'app\...\Handlers1'=>'test',//可指定多个类下的事件处理器
    );
    $this->handle([],'get',false);//指定get请求,且不使用事务提交
}

} (4)自定义接受参数(主要用于action之间的调用,即runAction($data)): use handlers\controllers\Controller; class TestController extends Controller {

public function actionIndex($data)
{
    $this->handlers = array(
        'app\...\Handlers'=>'test',
        ['app\...\Handlers'=>'test1'],//绑定同一个类下的多个事件处理器的时候,用[]避免key重复
        'app\...\Handlers1'=>'test',//可指定多个类下的事件处理器
    );
    $this->handle($data);//这里接受到的参数是$data;
}

}

6.  返回格式:

array(

'ret'=>0,
'msg'=>'OK',
'errorcode'=>0,
'data'=>[]

) ret:0成功,1失败 errorcode:失败时的错误码 msg:返回提示 data:返回数据