php - Yii2:多个事件附加在 View 上

标签 php events yii2

你好
我想尝试制作一些动态侧边栏和内容,我多次看到触发事件,
这是我的代码:

backend/view/layouts/_sidebar.php

use common\component\Hook;
use yii\base\Event;

Event::trigger(Hook::className(), Hook::SIDEBAR_MENU);

后端/view/ext/index.php

use common\component\Hook;
use yii\base\Event;

Event::trigger(Hook::className(), Hook::PlUGIN_CONTENT);

common/component/Hook.php

namespace common\component;

use yii\base\Component;

class Hook extends Component{

    const SIDEBAR_MENU = '';
    const PlUGIN_CONTENT = '';

}

后端\插件\插件\index.php

namespace backend\plugins\Plugin;

use Yii;
use common\component\Hook;
use yii\base\Event;
use yii\helpers\Html;

class Index extends Event{
    function sidebar() {
        echo '<li>' . Html::a('Menu Plugin 1', ['/ext?n=Plugin']) . '</li>';
    }

    function renderContent(){
        echo 'this is content';
    }
}

Event::on(Hook::className(), Hook::SIDEBAR_MENU, [new Index, 'sidebar']);
Event::on(Hook::className(), Hook::PlUGIN_CONTENT, [new Index, 'renderContent']);

后端\插件\Plugin2\index.php

namespace backend\plugins\Plugin2;

use Yii;
use common\component\Hook;
use yii\base\Event;
use yii\helpers\Html;

Event::on(Hook::className(), Hook::SIDEBAR_MENU, function () {
    echo '<li>' . Html::a('Menu Plugin 2', ['/ext?n=Plugin2']) . '</li>';
});

这里是我的 Controller backend/controller/ExtController.php

namespace backend\controllers;

use Yii;
use yii\web\Controller;

class ExtController extends Controller
{
    public function actionIndex(){    
        return $this->render('index');
    }

}

问题:

  1. 为什么它们在_sidebar.php中多次出现?我只是在sidebar.php中触发SIDEBAR MENU,为什么它们同时触发SIDEBAR_MENU和PLUGIN_CONTENT?
  2. 我在 backend/view/ext/index.php 中也有触发事件,但是 PLUGIN_CONTENT 没有触发,所以内容没有出现

最佳答案

在hook.php中,它们都有相同的值,都是空字符串"",它们一定有值

namespace common\component;

use yii\base\Component;

class Hook extends Component{

    const SIDEBAR_MENU = 'sidebarMenu';
    const PlUGIN_CONTENT = 'pluginContent';

}

关于php - Yii2:多个事件附加在 View 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847013/

相关文章:

php - Symfony 2.4 : Why are 500 errors not caught by kernel. 异常监听器

php - 在 Yii2 Rest Api 中使用 Yii2-user 进行用户注册

php - ZF2 弃用 : ServiceManagerAwareInterface

php - 垂直对齐 css 元素更靠近图像

javascript - 添加 JavaScript settimeout 函数

c# - 如何使事件回调进入我的 win 表单线程安全?

file-upload - 上传后 Yii2 文件验证不起作用

module - Yii2 模块模型

PHP如何发送USR1信号进行处理?

php - 获取给定日期一周内的所有工作日