如何在多模块/布局Zend Framework应用程序中将变量传递到布局? [英] How do I pass variables to a layout in a multiple module / layout Zend Framework application?

查看:93
本文介绍了如何在多模块/布局Zend Framework应用程序中将变量传递到布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将Zend Framework应用程序转换为基于模块的结构之前,它只有一个布局,可以像这样从控制器将变量传递给它:

Before I converted my Zend Framework application to a module based structure, it had a single layout and I could pass variables to it from my controller like so:

// Controller action
$this->view->foo = 'Something';

// Layout
<?= $this->foo ?>

但是,由于将所有内容移至默认模块并创建了单独的"admin"模块,因此我无法再使用该功能,这很可能是由于将视图设置"移出了引导文件并移入了控制器我在其中根据模块切换布局的插件.我的插件如下:

However, since moving everything into a default module and creating a separate "admin" module, I can no longer get this to work, most likely due to me moving my "View Settings" out of my bootstrap file and into the controller plugin where I am switching the layout based on the module. My plugin looks like this:

class KW_Controller_Plugin_LayoutSelector extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();
        $layout->setLayout($request->getModuleName());

        $view = $layout->getView();
        $view->doctype('HTML5');
        $view->setEncoding('UTF-8');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
        $view->headScript()->appendFile('/js/jquery-1.7.1.min.js');

        switch ($request->getModuleName()) {
            case 'admin':
                $view->headTitle('Admin Area')->setSeparator(' - ');
                $view->headLink()->appendStylesheet('/css/admin/global.css');
                $view->headScript()->appendFile('/js/admin/common.js');
                break;
            default:
                $view->headTitle('Main Site')->setSeparator(' - ');
                $view->headLink()->appendStylesheet('/css/global.css');
                $view->headScript()->appendFile('/js/common.js');
                break;
        }
    }
}

如果我将对视图的所有这些方法调用都移回引导程序,则可以再次将变量传递给布局;所以我猜测某些事情发生在错误的顺序中,也许是在我将变量从控制器传递到视图并且他们没有将其放入我的布局之后切换了我的布局? (我已经尝试通过将代码放入preDispatch()和postDispatch()等来更改上面的运行点)

If I move all of those method calls to the view back into my bootstrap, I can pass variables to the layout again; so I'm guessing that something is happening in the wrong order, maybe my layout is being switched after I have passed the variables to the view from my controller and they're not making it into my layout? (I've tried changing the point at which the above runs by putting the code in both preDispatch() and postDispatch(), etc.)

值得注意的是,我可以在各自的视图脚本中访问这些变量,而不能访问其中包含的布局.

It's worth noting, that I can access these variables in my individual view scripts, just not the layout that they're contained within.

任何指针将不胜感激.

推荐答案

我在当前项目中正在做类似的事情,并且基本上可以正常工作.

I'm doing something similar in a current project and it's largely working fine.

我注意到的一个区别是,我使用标准的应用程序资源在应用程序级别上同时启动了Layout和View.

One difference I note is that I am bootstrapping both Layout and View at the app-level using the standard application resources.

然后在我的插件中,我使用以下方法访问视图:

Then in my plugin, I access the view using:

$front = Zend_Controller_Front::getInstance();
$view = $front->getParam('bootstrap')->getResource('view');

这似乎可以保证我在引导程序,插件和控制器中访问相同的视图实例.

This seems to guarantee I am accessing the same view instance at bootstrap, in plugins, and in controllers.

这篇关于如何在多模块/布局Zend Framework应用程序中将变量传递到布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆