我从其他文章中获得了一些用于在zend框架中配置模块和布局的代码.我在当地尝试过.我没有得到默认和管理模块的不同布局.这是我为zend框架配置模块和布局的代码.
CONFIGS /的application.ini
[production] # Debug output phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 # Include path includePaths.library = APPLICATION_PATH "/../library" # Bootstrap bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" admin.bootstrap.path = APPLICATION_PATH "/modules/admin/Bootstrap.php" admin.bootstrap.class = "admin_Bootstrap" # Front Controller resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.frontController.env = APPLICATION_ENV # Session resources.session.name = "ZendSession" resources.session.save_path = APPLICATION_PATH "/../data/session" resources.session.remember_me_seconds = 86400 # Layout resources.layout.layout = "layout" resources.layout.layoutPath = APPLICATION_PATH "/layouts" admin.resources.layout.layout = "admin" admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts" # Views resources.view.encoding = "UTF-8" resources.view.basePath = APPLICATION_PATH "/views/" resources.view[] = resources.frontController.moduleDirectory = APPLICATION_PATH "/modules" resources.modules[] = resources.view[] = admin.resources.view[] = [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1
应用程序/ bootstrap.php中
bootstrap( 'view' ); $view = $this->getResource( 'view' ); $view->navigation = array(); $view->subnavigation = array(); $view->headTitle( 'Module One' ); $view->headLink()->appendStylesheet('/css/clear.css'); $view->headLink()->appendStylesheet('/css/main.css'); $view->headScript()->appendFile('/js/jquery.js'); $view->doctype( 'XHTML1_STRICT' ); //$view->navigation = $this->buildMenu(); } /*protected function _initAppAutoLoad() { $autoloader = new Zend_Application_Module_Autoloader(array( 'namespace' => 'default', 'basePath' => APPLICATION_PATH )); return $autoloader; }*/ protected function _initLayoutHelper() { $this->bootstrap('frontController'); $layout = Zend_Controller_Action_HelperBroker::addHelper( new ModuleLayoutLoader()); } public function _initControllers() { $front = Zend_Controller_Front::getInstance(); $front->addModuleDirectory(APPLICATION_PATH . '/modules/admin/', 'admin'); } protected function _initAutoLoadModuleAdmin() { $autoloader = new Zend_Application_module_Autoloader(array( 'namespace' => 'Admin', 'basePath' => APPLICATION_PATH . '/modules/admin' )); return $autoloader; } protected function _initModuleutoload() { $autoloader = new Zend_Application_Module_Autoloader ( array ('namespace' => '', 'basePath' => APPLICATION_PATH ) ); return $autoloader; } } class ModuleLayoutLoader extends Zend_Controller_Action_Helper_Abstract // looks up layout by module in application.ini { public function preDispatch() { $bootstrap = $this->getActionController() ->getInvokeArg('bootstrap'); $config = $bootstrap->getOptions(); echo $module = $this->getRequest()->getModuleName(); /*echo "Configs :"; print_r($config[$module]);*/ if (isset($config[$module]['resources']['layout']['layout'])) { $layoutScript = $config[$module]['resources']['layout']['layout']; $this->getActionController() ->getHelper('layout') ->setLayout($layoutScript); } } }应用/模块/管理/ bootstrap.php中
'admin', 'basePath' => APPLICATION_PATH . '/modules/admin/' )); return $autoloader; }*/ protected function _initDoctype() { $this->bootstrap( 'view' ); $view = $this->getResource( 'view' ); $view->navigation = array(); $view->subnavigation = array(); $view->headTitle( 'Module One' ); $view->headLink()->appendStylesheet('/css/clear.css'); $view->headLink()->appendStylesheet('/css/main.css'); $view->headScript()->appendFile('/js/jquery.js'); $view->doctype( 'XHTML1_STRICT' ); //$view->navigation = $this->buildMenu(); } }请仔细阅读并让我知道如何以正确的方式配置模块和布局.
感谢致敬,
Prasanth P.
1> DavidHavl..:我使用插件方法编写了这个代码:
在主引导程序中:
protected function _initPlugins() { // Access plugin $front = Zend_Controller_Front::getInstance(); $front->registerPlugin(new MyApp_Plugin_Module()); }在插件目录中:
class MyApp_Plugin_Module extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $module = $request->getModuleName(); $layout = Zend_Layout::getMvcInstance(); // check module and automatically set layout $layoutsDir = $layout->getLayoutPath(); // check if module layout exists else use default if(file_exists($layoutsDir . DIRECTORY_SEPARATOR . $module . ".phtml")) { $layout->setLayout($module); } else { $layout->setLayout("default"); } }希望能帮助到你.
2> P.M..:从您的代码:
# Layout resources.layout.layout = "layout" resources.layout.layoutPath = APPLICATION_PATH "/layouts" admin.resources.layout.layout = "admin" admin.resources.layout.layoutPath = APPLICATION_PATH "/modules/admin/layouts"你使用your_app/modules/admin/layouts/admin.phtml作为管理模块布局,我猜它取代了your_app/layouts/layout.phtml.检查一种在模块之间切换的方法,并尝试使用site.ressources.layout而不是resources.layout.layout.我是zend的新手.在http://www.survivethedeepend.com/上查看如何设置引导程序
这里强调了同样的问题和解决方案:http://blog.astrumfutura.com/archives/415-Self-Contained-Reusable-Zend-Framework-Modules-With-Standardised-Configurators.html
3> 小智..:在我的应用程序中,我这样配置.它工作得很好.
protected function _initLayout(){ $layout = explode('/', $_SERVER['REQUEST_URI']); if(in_array('admin', $layout)){ $layout_dir = 'admin'; }else if(in_array('default', $layout)){ $layout_dir = 'default'; }else{ $layout_dir = 'default'; } $options = array( 'layout' => 'layout', 'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts" ); Zend_Layout::startMvc($options); }