Zend Layout - 一个“智能”布局布局选择器

发布于 2024-08-14 05:24:24 字数 647 浏览 11 评论 0原文

我目前有 Zend 设置来在每个模块的 view/scripts/layout.phtml 文件(即:/application/modules/moduleName/scripts/layout.phtml)中查找布局脚本。这是通过在 application.ini 文件 (resources.layout[] = ) 中将layout[] 设置为空(空白)来实现的。

问题是许多模块可能共享相同的布局。我不想将相同的布局复制到使用它的每个模块中。我知道我可以通过设置像 resources.layout.layoutpath = /layoutPath 这样的特定路径来将所有内容设置为使用一个布局脚本,并且所有内容都将使用 /layoutpath/layout.phtml,而且我知道我可以设置单独的页面(或整个控制器,在 init 中)使用 $this->_helper->layout->setLayout('foobaz');

问题是某些模块将具有不同的布局,除了“标准”之外,我不想根据控制器或操作来设置它。我想为整个模块设置它,设置在一个地方(或者通过代码/Zend自动直观地计算出来)。理想情况下,它将按照当前的方式设置,但如果模块没有自己的layout.phtml,它将使用默认模块的布局。

那么...我该怎么做呢?

I currently have Zend setup to look for a layout script in each module's view/scripts/layout.phtml file (ie: /application/modules/moduleName/scripts/layout.phtml). This is by setting layout[] to nothing (blank) in the application.ini file (resources.layout[] = )

The issue is that many modules may share the same layout. I don't want to copy the same exact layout into each module that uses it. I know I can set everything to use one layout script by setting a specific path like resources.layout.layoutpath = /layoutPath and everything would use /layoutpath/layout.phtml, and I know I can set individual pages (or whole Controllers, in the init) by using $this->_helper->layout->setLayout('foobaz');

The issue is that some modules will have different layouts, other than the 'standard' one, and I don't want to set it on a by Controller or by Action basis. I want to set it for the entire module, set in one place (or intuitively figured out by code/Zend automatically). Ideally, it would be setup how it is currently, but if a module doesn't have its own layout.phtml, it would use the default module's layout.

So... how do I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿忘初心 2024-08-21 05:24:24

有几种解决方案,选择适合自己的策略

1 扩展动作控制器

class App_Controller_Action extends Zend_Controller_Action 
{

    public function init()
    {
        parent::init();

        $moduleName = $this->getRequest()->getModuleName();
        $layoutPath = APPLICATION_PATH . '/modules/' . $moduleName . '/layouts';
        if (is_dir($layoutPath)) {
            $this->view->addScriptPath($layoutPath);
        }    
    }
 }

然后照常做 IndexController extends App_Controller_Action ...
如果布局文件存在于 APPLICATION_PATH 中。 '/模块/' 。 $模块名称。 '/layouts' 目录 - 它将代替默认布局

2 使用,您可以编写 frontcontroller 插件

class App_Controller_Plugin_ModuleSwitcher extends Zend_Controller_Plugin_Abstract
 {
     protected $_view = null;

     public function routeShutdown(Zend_Controller_Request_Abstract $request)
     {
         $moduleName = $request->getModuleName();

         Zend_Layout::startMvc();
         $layout = Zend_Layout::getMvcInstance();
         $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $moduleName . '/layouts')->setLayout($moduleName);

         return $request;
     }
 }

,并且不要忘记 google 寻找其他解决方案;)

There are several solutions, choose their own strategy

1 extending the action controller

class App_Controller_Action extends Zend_Controller_Action 
{

    public function init()
    {
        parent::init();

        $moduleName = $this->getRequest()->getModuleName();
        $layoutPath = APPLICATION_PATH . '/modules/' . $moduleName . '/layouts';
        if (is_dir($layoutPath)) {
            $this->view->addScriptPath($layoutPath);
        }    
    }
 }

and then do as usual IndexController extends App_Controller_Action ...
if layout file exists in APPLICATION_PATH . '/modules/' . $moduleName . '/layouts' directory - it will ne used instead of default layout

2 you can write frontcontroller plugin

class App_Controller_Plugin_ModuleSwitcher extends Zend_Controller_Plugin_Abstract
 {
     protected $_view = null;

     public function routeShutdown(Zend_Controller_Request_Abstract $request)
     {
         $moduleName = $request->getModuleName();

         Zend_Layout::startMvc();
         $layout = Zend_Layout::getMvcInstance();
         $layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $moduleName . '/layouts')->setLayout($moduleName);

         return $request;
     }
 }

and dont forget to google for another solutions ;)

╰◇生如夏花灿烂 2024-08-21 05:24:24

您可以通过几个步骤设置自己的布局选择器

第 1 步:
使模块成为管理员和默认模块。

步骤2:
在每个模块中创建布局文件夹作为 admin/layouts/scripts
和默认/布局/脚本
放入layout.phtml

第3步:
从Application/layouts/scripts 中删除layout.phtml 文件。

第4步:
在库中创建 Plugin 文件夹并创建 Plugin.php
步骤

class Plugin_Layout extends Zend_Controller_Plugin_Abstract 
{

   public function preDispatch(Zend_Controller_Request_Abstract $request)

    {
        $layoutPath = APPLICATION_PATH . '/modules/' . $request->getModuleName() . '/layouts/scripts/';
        Zend_Layout::getMvcInstance()->setLayoutPath($layoutPath);
    }
}   

5:

打开Application/configs/Appication.ini文件
并编辑它
步骤

;resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout"
;register your plugin

autoloaderNamespaces[] = "Plugin"
resources.frontController.plugins[] = "Plugin_Layout"

6:

打开引导文件Application/Bootstrap
把代码放进去

protected function _initAutoload()

 {

        $loader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => APPLICATION_PATH . '/modules/'
                ));

        return $loader;
    }

    protected function _initPlugins()

{

        $this->bootstrap('frontcontroller');
        $fc = $this->getResource('frontcontroller');
        $fc->registerPlugin(new Plugin_Layout());
}

you can set own layout selector in few steps

step 1:
make module admin and default.

step 2:
create layout folder in each module as admin/layouts/scripts
and default/layouts/scripts
put into layout.phtml

step 3:
delete the layout.phtml file from Application/layouts/scripts.

step 4:
make the the Plugin folder inside library and make Plugin.php
as

class Plugin_Layout extends Zend_Controller_Plugin_Abstract 
{

   public function preDispatch(Zend_Controller_Request_Abstract $request)

    {
        $layoutPath = APPLICATION_PATH . '/modules/' . $request->getModuleName() . '/layouts/scripts/';
        Zend_Layout::getMvcInstance()->setLayoutPath($layoutPath);
    }
}   

step 5:

open Application/configs/Appication.ini file
and edit it
as

;resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout"
;register your plugin

autoloaderNamespaces[] = "Plugin"
resources.frontController.plugins[] = "Plugin_Layout"

Step 6:

open bootstrap file Application/Bootstrap
put the code inside

protected function _initAutoload()

 {

        $loader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => APPLICATION_PATH . '/modules/'
                ));

        return $loader;
    }

    protected function _initPlugins()

{

        $this->bootstrap('frontcontroller');
        $fc = $this->getResource('frontcontroller');
        $fc->registerPlugin(new Plugin_Layout());
}
药祭#氼 2024-08-21 05:24:24

最快的解决方案可能是创建一个符号链接,将模块布局文件指向默认布局。这在 Windows 上不起作用并且更难维护。

更好的是,在 Bootstrap 中创建一个方法来设置布局。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLayoutScript(){
        //ensure layout is setup
        $this->bootstrap(array('layout', 'FrontController'));

        $layout= $this->getResource('layout');

        $front = $this->getResource('FrontController');

        //do something with $layout and $front - set layout script/path etc based on request 
        //You could use file_exists to detect module layout scripts 

    }

}

请参阅 http:// /framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources 了解更多详细信息。

最后,您可以 编写您自己的应用程序资源以与 Zend_Application 一起使用。

The quickest solution might be to create a symlink to point what would be a module layout file to the default layout. This won't work on Windows and is harder to maintain.

Better, create a method in your Bootstrap to set the layout.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    public function _initLayoutScript(){
        //ensure layout is setup
        $this->bootstrap(array('layout', 'FrontController'));

        $layout= $this->getResource('layout');

        $front = $this->getResource('FrontController');

        //do something with $layout and $front - set layout script/path etc based on request 
        //You could use file_exists to detect module layout scripts 

    }

}

See http://framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources for more details.

Finally, you could write your own application resource for use with Zend_Application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文