嗨,
我想尝试制作一些动态的侧边栏和内容,我在视图中多次触发事件,
在这里我的代码:
后端/视图/布局/ _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);
公共/组件/ 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 / ExtController.php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
class ExtController extends Controller
{
public function actionIndex(){
return $this->render('index');
}
}
问题:
答案 0 :(得分:2)
在hook.php中,它们都具有相同的值,即空字符串“”,它们具有值
namespace common\component;
use yii\base\Component;
class Hook extends Component{
const SIDEBAR_MENU = 'sidebarMenu';
const PlUGIN_CONTENT = 'pluginContent';
}