【发布时间】:2015-04-21 07:44:24
【问题描述】:
我想更改 /protected/views/layouts/main.php 中的顶部菜单,以便将其存储在数据库中。我想这样做:在 /protected/config/main.php 中添加从那里返回菜单项数组的数组:
return array(
'basePath' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..',
'name' => 'My Web Application',
'preload' => array('log'),
'menu' => array(
array('label' => 'Home', 'url' => array('/site/index')),
array('label' => 'About', 'url' => array('/site/page')),
array('label' => 'Contact', 'url' => array('/site/contact'))
),
但是菜单项应该像菜单模型类的对象一样从数据库中获取。我感兴趣的问题是如何从 /protected/config/main.php 访问模型。如果我写 /protected/config/main.php 之类的东西
$types = PageType::model()->findAll();
其中 PageType 是现有模型类,我得到一个错误:
警告: 包括(PageType.php)[function.include]:无法打开流:否 这样的文件或目录在 Z:\home\localhost\www\yii-1.1.16.bca042\framework\YiiBase.php 上线 432
警告:include() [function.include]:无法打开“PageType.php” 用于包含 (include_path='.;/usr/local/php5/PEAR') Z:\home\localhost\www\yii-1.1.16.bca042\framework\YiiBase.php 上线 432
致命错误:在中找不到类“PageType” Z:\home\localhost\www\mycms\protected\config\main.php 在第 8 行
我不想直接从这个脚本连接到数据库。
【问题讨论】:
-
为什么要在 protected/config/main 上获取菜单数据?在 protected/config/main 中,所有框架组件尚未初始化。我建议您更改创建组件的方法,例如 protected/components/MainMenu,并在您的 protected/views/layouts/main 上使用它。在这个组件中,您可以执行任何操作,例如获取数据、构建菜单并呈现它。
-
感谢您的建议。组件是我真正需要的。
-
全部完成。真的很酷,现在我可以从自动生成的表单中添加菜单项了。
-
不错!请自动回答问题并关闭它