Yii2:多事件附加在视图上 [英] Yii2: multiple event attach on view
本文介绍了Yii2:多事件附加在视图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨,
我想尝试制作一些动态边栏和内容,我有触发事件多次查看,
这里我的代码:
Hi,
I want try to make some dynamic sidebar and content, I have trigger event multiple times in view,
here my code:
后端/查看/布局/ _sidebar.php
use common\component\Hook;
use yii\base\Event;
Event::trigger(Hook::className(), Hook::SIDEBAR_MENU);
后端/视图/ 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 = '';
}
backend\plugins\Plugin\ 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']);
backend\plugins\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>';
});
这里我的控制器后端/控制器/ ExtController.php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
class ExtController extends Controller
{
public function actionIndex(){
return $this->render('index');
}
}
问题: strong>
question:
- 为什么他们在_sidebar.php中多次出现?我只是在sidebar.php中触发SIDEBAR MENU,为什么他们触发SIDEBAR_MENU在同样的时间里,PLUGIN_CONTENT?
- 我在后端/ view / ext / index.php中也有触发事件,但是没有触发PLUGIN_CONTENT,所以内容不会出现
推荐答案
在hook.php中,它们都具有相同的值,即空字符串,它们有值
in hook.php, they both have same value, which is empty string "", they mush have value
namespace common\component;
use yii\base\Component;
class Hook extends Component{
const SIDEBAR_MENU = 'sidebarMenu';
const PlUGIN_CONTENT = 'pluginContent';
}
这篇关于Yii2:多事件附加在视图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文