ffchen 2016-10-24 10:09:48 3301次浏览 0条评论 0 0 0
/**
 * 过滤器就是通过行为触发事件方式把 自己有过滤功能的类附加给控制器让他给过滤
 * 行为注入,可以在不修改现有类的代码的情况下,更改、扩展类对于事件的响应和支持
 * 这个行为(行为就是继承Bahavior的子类,子类中包含了所要注入的属性和方法)有点特殊他重写了Bahavior类的attach方法
 * 这个过滤类->他继承了 base\ActionFilter类->他继承了Bahavior类他就是行为,就是ActionFilter类重写了attach方法
 * public function attach($owner)
 * {
 *      $this->owner = $owner;
 *      $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
 * }
 * 从上边代码看到他将ActionFilter类beforeFilter方法绑定给Controller::EVENT_BEFORE_ACTION事件
 * 当控制器的EVENT_BEFORE_ACTION事件触发将会执行ActionFilter类beforeFilter方法
 * public function beforeFilter($event)
 * {
 *  if (!$this->isActive($event->action)) {
 *       return;
 *  }
 * //执行子类(过滤类他首先要继承base\ActionFilter类)的beforeAction()方法如
 *  $event->isValid = $this->beforeAction($event->action);
 *  if ($event->isValid) {
 *      $this->owner->on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false);
 *  }else{
 *     $event->handled = true;
 *  }
 * }
 * 触发过程比较简单
 * 每个Yii请求都会执行base\Controller的runAction在此方法会执行$this->beforeAction($action)方法
 * public function beforeAction($action)
 * {
 *      $event = new ActionEvent($action);
 *      $this->trigger(self::EVENT_BEFORE_ACTION, $event);
 *      return $event->isValid;
 * }
 * //这个方法就触发了EVENT_BEFORE_ACTION事件
 */
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册