重定向不同的视图脚本

问题描述 投票:0回答:2

我想重定向到不同的viewscripts取决于用户可以填写的searchtype。

例如:用户想要搜索一个人,而不是我想使用人的匹配视图(ansprechpartner)。请查看我的控制器操作的一部分:

 switch ($suche['suchtyp']) {
            case 1:            //Ansprechpartner
                $view = new ViewModel([
                   'ansprechpartner' => $this->ansprechpartnerTable->sucheAnsprechpartner($suche['suche']),
                        ]);
                $view->setTemplate('ansprechpartner/index');
                return $view;
                break;
            case 2:            //Mandant
                $view = new ViewModel([
                   'mandant' => $this->mandantTable->sucheMandant($suche['suche']),
                ]);
                $view->setTemplate('mandant/index');
                return $view;
                break;
            case 3:            //vertrag
                $view = new ViewModel([
                   'vertrag' => $this->vertragTable->sucheVertrag($suche['suche']),
                ]);
                $view->setTemplate('vertrag/index');
                return $view;
                break;

            default:
                return $this->redirect()->toRoute('index', ['action' => 'index']);
        }

在屏幕截图中,我的文件夹将显示:

enter image description here

那么在这种情况下如何使用现有的viewscripts而不调用匹配的控制器动作呢?

zend-framework zend-framework3 zend-view zend-router
2个回答
2
投票

我认为你应该在你的setTemplate中提供switch的完整模板路径

    $view = new ViewModel([
                   'ansprechpartner' => $this->ansprechpartnerTable->sucheAnsprechpartner($suche['suche']),
                        ]);
   $view->setTemplate('stammdaten/ansprechpartner/index');
   return $view;

0
投票

此开关应位于Action(在Controller中)。这是那种永远不应该被看到的逻辑。但是,如果您确实在动作中使用了它,则可以将ZF用于set a different layout

链接示例:

// A controller's action method that uses an alternative
// layout template.
public function indexAction() 
{
  //...

  // Use the Layout plugin to access the ViewModel
  // object associated with layout template.
  $this->layout()->setTemplate('layout/layout2');

  //...
}
© www.soinside.com 2019 - 2024. All rights reserved.