pia-sophie
pia-sophie

Reputation: 515

redirect different view scripts

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?

Upvotes: 0

Views: 42

Answers (2)

Gautam Rai
Gautam Rai

Reputation: 2505

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;

Upvotes: 2

rkeet
rkeet

Reputation: 3468

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

  //...
}

Upvotes: 0

Related Questions