你好,
我想尝试制作一些动态侧边栏和内容,我多次看到触发事件,
这是我的代码:
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');
}
}
问题:
- 为什么它们在_sidebar.php中多次出现?我只是在sidebar.php中触发SIDEBAR MENU,为什么它们同时触发SIDEBAR_MENU和PLUGIN_CONTENT?
- 我在 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/