CakePHP 3实现的事件配置数组覆盖问题 [英] CakePHP 3 implementedEvents config array overwrite issue

查看:65
本文介绍了CakePHP 3实现的事件配置数组覆盖问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是示例代码:

    class UsersController extends AppController
    {
        ...
        public function implementedEvents()
        {
            return [
                'Auth.logout' => 'afterLogout'
            ];
        }

        public function afterLogout($event)
        {
            $this->Flash->toast(__('Good bye!'));
        }
        ...
    }

在实施 implementedEvents()方法之前,已正确触发 AppController :: beforeRender()方法.我需要听 Auth.logout 事件,所以写了 implementedEvents()方法.我认为它将被合并到默认事件数组.但是之后, AppController :: beforeRender()停止工作.它不再触发.所以我想这是一种覆盖行为.

Before implementing implementedEvents() method, AppController::beforeRender() method was triggered properly. I needed to listen to Auth.logout event, so wrote implementedEvents() method. I thought it would be merged to the default events array. But after that, AppController::beforeRender() stopped working. It didn't trigger any more. So I guess there's an overwriting behavior.

这是CakePHP 3的默认行为吗?这是预期的行为还是错误?

Is this a default behavior of CakePHP 3? And is this an intended behavior or a bug?

推荐答案

这是预期的行为,否则覆盖而不是合并会很复杂.

That's the intended behavior, as otherwise it would be complicated to overwrite instead of merging.

如果您需要合并可能的父侦听器配置,则需要自己完成,例如

If you need to merge possible parent listener configuration, then you need to do that on your own, like

public function implementedEvents()
{
    return [
        'Auth.logout' => 'afterLogout'
    ] + parent::implementedEvents();
}

这篇关于CakePHP 3实现的事件配置数组覆盖问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆