【问题标题】:Change layout in different views under the same controller - zend framework同一个控制器下不同视图改变布局——zend框架
【发布时间】:2011-05-13 12:07:18
【问题描述】:

我有一个带有许多视图/操作的控制器。我使用 zend_navigation 和面包屑作为主导航。但是,当呈现某些视图/操作时,我想在这组视图/操作中添加一个带有本地导航的“控制面板”。添加面板没问题,这是决定我在哪个视图/动作中的逻辑。

我可以从控制器级别识别视图/操作

    $controller = $this->getRequest()->getControllerName();
    $action = $this->getRequest()->getActionName();

但是我可以将它传递给布局,还是我需要或者我可以检测我在布局中的位置? 我想一种选择是在单独的控制器中使用控制面板创建视图,但这似乎是一种愚蠢的做法。

【问题讨论】:

    标签: zend-framework


    【解决方案1】:

    我认为你必须结合 ArneRie 和 Acherer 所说的:

    在 /application/configs/cp.ini 内部:

    cp[] = "moduleA.controllerB.actionC"
    cp[] = "moduleA.controllerD.actionE"
    

    在您的引导程序中:

    protected function _initCp()
    {
      $ini = new Zend_Config_Ini(APPLICATION_PATH . '/configs/cp.ini');
      Zend_Registry::set('cp', $ini->toArray());
    }
    

    在你的布局内:

    <?= $this->partial('cp.phtml') ?>
    

    在你的部分:/application/layouts/cp.phtml:

    <?php
    $request = Zend_Controller_Front::getInstance()->getRequest();
    $module = $request->getModuleName();
    $controller = $request->getControllerName();
    $action = $request->getActionName();
    if (in_array(implode('.', array($module, $controller, $action)), Zend_Registry::get('cp')) : ?>
        // display cp html here
    <?php endif ?>
    

    您可能需要仔细检查 ini 数组,但这是 Zend Jedi 的路径:p

    【讨论】:

      【解决方案2】:

      只是一个想法,我会在 ControllerPlugin 中执行此操作

      public function preDispatch()
      {
          $controller = $this->getRequest()->getControllerName();
          $action = $this->getRequest()->getActionName();
      
          $layout = Zend_Layout::getMvcInstance();
          $layoutView = $layout->getView();
      
          $whereIam = $controller. '.' .$action;
      
          switch ($whereIam) {
              case 'index.showuser':
                  $layoutView->showPanel = true;
                  $layoutView->controlPanel = $this->view->render('myPanel.phtml');
                  break;
              default:
                  break;
          }
      }
      
      
         //layout.phtml
          if ($this->showPanel === true) {
              echo $this->controlPanel;
          }
      

      【讨论】:

        【解决方案3】:

        这可以工作,或者只是在需要它的操作上传递$this-&gt;view-&gt;cp = true;,并检查 layout.phtml 文件以查看其设置是否为真

        【讨论】:

          猜你喜欢
          • 2017-04-20
          • 1970-01-01
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 2014-02-06
          • 2012-07-19
          • 2010-11-07
          • 1970-01-01
          相关资源
          最近更新 更多