Zend framework zend框架中的模块配置和布局配置
我从其他文章中获得了一些代码,用于在zend框架中配置模块和布局。我试过在我的本地。我没有得到默认和管理模块不同的布局。下面是我为zend framework配置模块和布局的代码 配置/application.iniZend framework zend框架中的模块配置和布局配置,zend-framework,Zend Framework,我从其他文章中获得了一些代码,用于在zend框架中配置模块和布局。我试过在我的本地。我没有得到默认和管理模块不同的布局。下面是我为zend framework配置模块和布局的代码 配置/application.ini [production] # Debug output phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 # Include path includePaths.library = APP
[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
您需要使用控制器插件来实现这一点,因为布局是基于请求条目设置的,并且在引导过程中应用程序尚未调度,因此您需要使用控制器插件来处理预剥离以切换布局。从您的代码:
protected function _initPlugins()
{
// Access plugin
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new MyApp_Plugin_Module());
}
您正在使用_app/modules/admin/layouts/admin.phtml作为管理模块布局,我猜它取代了_app/layouts/layout.phtml。检查在模块之间切换的方法,然后尝试使用site.resources.layout而不是resources.layout.layout。我是zend的新手。查看如何在
这里也强调了同样的问题和解决方案:我使用插件方法编写了以下代码:
在主引导中:
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");
}
}
在插件目录中:
protected function _initLayout(){
$layout = explode('/', $_SERVER['REQUEST_URI']);
if(in_array('admin', $layout)){
$layout_dir = 'admin';
}else{
$layout_dir = 'site';
}
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH . "/layouts/scripts/".$layout_dir,
);
Zend_Layout::startMvc($options);
}
希望有帮助。我认为最简单的方法是检查URI\u字符串。请参阅下文:
我有一个名为“admin”的模块。
在布局文件夹下,我有2个目录“站点”和“管理员”
\application\layout\site\layout.phtml和\application\layout\admin\layout.phtml
在Bootstrap.php上添加这段代码
它只是更改布局目录路径
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$moduleLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules'
)
);
return $moduleLoader;
}
protected function _initViewhelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);
$view->navigation($navigation);
}
}
导航($navigation);
}
}
您的问题回答了我的问题,没错,我正试图找出它在我的引导模块中不起作用的原因,从您需要添加行的配置文件中可以看出
administrador.resources.view[]=
代客泊车伙伴 在我的应用程序中,我是这样配置的。它工作得很好
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
);
Zend_Layout::startMvc($options);
未在新的zend项目上启用布局和模块输入(ZF版本1中的)。它需要被启用,你需要让它工作
布局适用于工作zend项目的公共页眉和页脚,另一方面,模块可用于不同类型的访问,即用户模块、管理员模块、访问者模块等
为了快速参考,你可以在我的网站上找到一个完整的项目的完整解释,从这里获得基本想法
祝你好运,干杯
从SOF尝试了一些其他解决方案,这一个效果很好。只需将布局路径指向实际布局的文件夹在包含此文件之前,还需要在引导程序中调用Zend_布局::startMvc()
,请不要使用Zend_控制器\u Front::getInstance()->getModuleDirectory()获取模块目录的路径
当我尝试这段代码时,我发现Zend_视图异常“找不到default.html”。我的文件名为layout.phtml。当我把它改为default.phtml时,它工作了。此外,特定于模块的布局需要存储在与模块同名的/application/layouts/scripts下。希望它能帮助那些还在想它是如何工作的人。谢谢。如果你想添加一些用户友好的重写内容,例如www.mysite.com/user-admin.htmlc,这将是一个坏主意。你能提供一点解释吗?我不明白。。你们为什么投票反对这个职位?。这篇文章几乎在谷歌搜索中被点击,并且被大多数人评论!!。。如果答案是错误的,请只投票给答案,而不是该职位。谢谢
protected function _initLayout(){
$layout = explode('/', $_SERVER['REQUEST_URI']);
if(in_array('admin', $layout)){
$layout_dir = 'admin';
}else{
$layout_dir = 'site';
}
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH . "/layouts/scripts/".$layout_dir,
);
Zend_Layout::startMvc($options);
}
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload() {
$autoloader = Zend_Loader_Autoloader::getInstance();
$moduleLoader = new Zend_Application_Module_Autoloader(
array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules'
)
);
return $moduleLoader;
}
protected function _initViewhelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
}
protected function _initNavigation()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
$navigation = new Zend_Navigation($config);
$view->navigation($navigation);
}
}
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);
}
$options = array(
'layout' => 'layout',
'layoutPath' => APPLICATION_PATH."/modules/".$layout_dir."/views/layouts"
);
Zend_Layout::startMvc($options);