dongzhuo3059 2012-04-09 14:24
浏览 50
已采纳

如何将变量传递给多模块/布局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 ?>

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;
        }
    }
}

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.

Any pointers would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dongzhi4239 2012-04-09 15:00
    关注

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致