Zend Framework 中可以有多个布局吗?

发布于 2024-10-05 13:02:53 字数 83 浏览 2 评论 0原文

我有一个华丽的页面,前端有图像旋转器供客户使用。

对于后端我想要不同的布局。我可以有多个布局吗?

一点提示将是值得赞赏的

I have a flashy page with image rotators in the front end for the clients.

For back-end I want to have different layout. Can i have multiple layout?

A little hint would be appreciable

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

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

发布评论

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

评论(4

杀お生予夺 2024-10-12 13:02:53

我创建了一个布局插件,以便在调用非默认模块时切换布局:

class MyApplication_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        switch ($request->getModuleName()) {
            case 'admin': $this->_moduleChange('admin');
        }
    }

    protected function _moduleChange($moduleName) {
        $this->getLayout()->setLayoutPath(
            dirname(dirname(
                $this->getLayout()->getLayoutPath()
            ))
            . DIRECTORY_SEPARATOR . 'layouts/scripts/' . $moduleName
        );
        $this->getLayout()->setLayout($moduleName);
    }

}

然后在我的 Bootstrap 中,我执行以下操作:

Zend_Layout::startMvc(
            array(
                'layoutPath' => self::$root . '/application/views/layouts/scripts',
                'layout' => 'layout',
                'pluginClass' => 'MyApplication_Layout_Controller_Plugin_Layout'
            )
        );

非默认布局位于以模块命名的文件夹内,因此我的目录结构如下所示:

/path/to/application/views/layouts/scripts/layout.phtml --> default layout

/path/to/application/views/layouts/scripts/admin/admin.phtml --> admin layout

I create a layout plugin, to switch layouts when a non-default module is called:

class MyApplication_Layout_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
{

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        switch ($request->getModuleName()) {
            case 'admin': $this->_moduleChange('admin');
        }
    }

    protected function _moduleChange($moduleName) {
        $this->getLayout()->setLayoutPath(
            dirname(dirname(
                $this->getLayout()->getLayoutPath()
            ))
            . DIRECTORY_SEPARATOR . 'layouts/scripts/' . $moduleName
        );
        $this->getLayout()->setLayout($moduleName);
    }

}

Then in my Bootstrap, I do this:

Zend_Layout::startMvc(
            array(
                'layoutPath' => self::$root . '/application/views/layouts/scripts',
                'layout' => 'layout',
                'pluginClass' => 'MyApplication_Layout_Controller_Plugin_Layout'
            )
        );

The non-default layouts go inside a folder named after the module, so my directory structure looks like this:

/path/to/application/views/layouts/scripts/layout.phtml --> default layout

/path/to/application/views/layouts/scripts/admin/admin.phtml --> admin layout
寄风 2024-10-12 13:02:53

是的,您可以有多种布局,尽管根据请求切换它们并不是那么简单。

我必须这样做足够多次,最终开发了一个控制器操作助手和应用程序资源插件,您可以自由使用或从中获取灵感。

ModuleLayout 应用程序资源插件

ModuleLayoutLoader 控制器操作助手

Yes, you can have multiple layouts though switching them based on the request is not so straight forward.

I've had to do this enough times that I ended up developing a controller action helper and application resource plugin that you're free to use or take inspiration from.

ModuleLayout Application Resource Plugin

ModuleLayoutLoader Controller Action Helper

请别遗忘我 2024-10-12 13:02:53

尝试

//in controller
$this->_helper->layout->setLayout('layoutName');

它将布局切换到模块的 view/scripts 文件夹中的layoutName.phtml ;)

try

//in controller
$this->_helper->layout->setLayout('layoutName');

It will switch layout to layoutName.phtml in your module's view/scripts folder ;)

慕巷 2024-10-12 13:02:53

这是错误的。该行:

class MyApplication_Layout_Controller_Plugin_Layout extends end_Layout_Controller_Plugin_Layout

应该是extends Zend_Controller_Plugin_Abstract。否则,您将收到有关 mvcSuccessfulActionOnly 的错误。

This is wrong. The line:

class MyApplication_Layout_Controller_Plugin_Layout extends end_Layout_Controller_Plugin_Layout

should be extends Zend_Controller_Plugin_Abstract. Otherwise, you'll get an error concerning mvcSuccessfulActionOnly.

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