0

I want to redirect to different viewscripts depends on a searchtype the user can fill in.

For example: The user wants to search for a person, than I want to use the matching viewscripts for persons (ansprechpartner). Please have a look at a part of my controller action:

 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']);
        }

In the screenshot my folders will be shown:

enter image description here

So how can I use the existing viewscripts in this case, without to call the matching controller actions?

pia-sophie
  • 505
  • 4
  • 21
  • The template path must always be complete. The correct path are"stammdaten/ansprechpartner/index", "stammdaten/mandant/index" and "stammdaten/vertrag/index" – Ermenegildo Apr 04 '19 at 08:42

2 Answers2

2

I think you should provide full template path to the setTemplate, in your switch

    $view = new ViewModel([
                   'ansprechpartner' => $this->ansprechpartnerTable->sucheAnsprechpartner($suche['suche']),
                        ]);
   $view->setTemplate('stammdaten/ansprechpartner/index');
   return $view;
Gautam Rai
  • 2,445
  • 2
  • 21
  • 31
0

This switch should be in the Action (in a Controller). It's the kind of logic that should never be in view. However, if you do have it in an action, you can leverage ZF to set a different layout

Example from link:

// 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');

  //...
}
rkeet
  • 3,406
  • 2
  • 23
  • 49
  • hm my posted code is the controller action. My problem is more like a routing issue, it is not about setting a layout. I'm not clear with the concept how to switch to the other controller actions. In my switch I want to redirect to for example mandant/index or ansprechpartner/index how can I do this giving the variable mandant, ansprechpartner etc. to the view? – pia-sophie Mar 27 '19 at 10:18
  • So, do you mean to [dispatch](https://zendframework.github.io/zend-mvc/controllers/) a user to another Controller (and thus a full-on redirect), or do you mean to execute another controller->action somewhere and use the result? – rkeet Mar 27 '19 at 10:28