Yii2 event-如何使用事件

这里说的是通过行为的方式绑定事件

  • 定义行为事件类:EventService.php
<?php
namespace api\components;

use Yii;
use yii\base\Behavior;
use yii\base\Application;

/**
 * 事件类
 */
class EventService extends Behavior
{
    // 定义事件名
    const EVENT_BEFORE_DEMO1 = 'beforeDemo1';

    /**
     * [ 事件绑定处理程序 ]
     * @return [type] [description]
     */
    public function events()
    {
        return [
            Application::EVENT_BEFORE_REQUEST => 'demo',
            self::EVENT_BEFORE_DEMO1 => 'demo1',
        ];
    }

    /**
     * [ 事件测试 ]
     * @param  [type] $event [description]
     * @return [type]        [description]
     */
    public function demo($event)
    {
        Yii::info(['message'=>'事件测试(自动触发)'], 'demo');
    }

    /**
     * [ 事件测试1 ]
     * @param  [type] $event [description]
     * @return [type]        [description]
     */
    public function demo1($event)
    {
        $log = [
            'message' => '事件测试1',
            'ip' => $event->ip,
            'route' => $event->route,
        ];
        Yii::info($log, 'demo1');
    }
}
  • 定义数据格式类:MyEvent.php
<?php
namespace api\components;

use Yii;
use yii\base\Model;
use yii\base\Event;

/**
 * ContactForm is the model behind the contact form.
 */
class MyEvent extends Event
{
    /** @var [type] [ 请求路由 ] */
    public $route;

    /** @var [type] [ 请求IP ] */
    public $ip;

}
  • 配置自动触发事件
在config/main.php中components同级定义
    'name' => 'My Api',
    'as behaviors' => 'api\components\EventService',
    'components' => [ ... ]
  • 在行为函数中手动触发行为事件
    /**
     * [ 首页 ]
     * @return [type] [description]
     */
    public function actionIndex()
    {
        $event = new MyEvent();
        // 传递参数
        $event->ip = Yii::$app->request->userIP;
        $event->route = Yii::$app->requestedRoute;

        // 手动触发事件
        Yii::$app->trigger(EventService::EVENT_BEFORE_DEMO1, $event);
    }
  • 查看日志
2020-08-05 14:43:47 [127.0.0.1][-][-][info][demo] [
    'message' => '事件测试(自动触发)',
]
    in E:\data\www\project\php\yii2\advanced\api\components\EventService.php:35
2020-08-05 14:43:47 [127.0.0.1][-][-][info][demo1] [
    'message' => '事件测试1',
    'ip' => '127.0.0.1',
    'route' => 'site/index',
]
    in E:\data\www\project\php\yii2\advanced\api\components\EventService.php:50
    in E:\data\www\project\php\yii2\advanced\api\controllers\SiteController.php:84
2020-08-05 14:43:47 [127.0.0.1][-][-][info][application] $_GET = []

Yii2 事件的使用就是这么简单,利用事件可以帮助我们完成很多功能。

const EVENT_BEFORE_REQUEST = 'beforeRequest',这个是yii\base\Application自带的事件,意思是"事件在应用程序开始处理请求之前引发的事件"。

有请求前的就肯定有请求后的事件。const EVENT_AFTER_REQUEST = 'afterRequest',意思是"事件在应用程序成功处理请求之后(在发出响应之前)引发的事件。"。

自定义的事件需要在特定需要的时候去触发。Yii::$app->trigger('定义事件名', 实例化event的类,可以传递参数,可以不传递)

以上就是对Yii2框架事件使用方法分享,各位大牛要是有更好的方法也请分享一下。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值