Typical Setup
Typically Zend Framework is setup to bind every controller’s action to a specific view. The names of the action and the view script must be the same, or at least must be similar – following the ZF’s convention.
Thus if you name an action, let’s say, indexAction(), you’ve to create an index.phtml file into the views/scripts/index/ folder. Let me remind you that this is what is called a typical setup, but this may vary from one installation to another.
Thus everything’s fine and the MVC does its best. After the user requests a specific uri, the framework parses it and finds the controller, than the action and than the view script – and there is a view script for every single action.
When there are two or more actions – there are two or more .phtml (view scripts) files.
Some Exceptions
Sometimes you have very similar logic, but very different markup between two actions. As the logic goes to the controller/action and the markup into the view, the question is can you simply have one action in the controller, but with two different .phtml file as a view scripts? Of course switching between those two (or more) with some conditionals.
Solution No. 1
However once I posted a simple, and working, solution. There you have again two actions and two scripts:
<?php
class IndexController extends Zend_Controller_Action
{
public function firstAction()
{
// place all the logic you need here
// and every view placeholder assignment
$this->view->title = "My Title";
// if the logic requires a different markup
// simply redirect to another view
if (some_expression) {
$this->_forward('second');
}
}
public function secondAction()
{}
}
// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>
// 2. second.phtml
<h2><?php echo $this->title ?></h2> |
<?php
class IndexController extends Zend_Controller_Action
{
public function firstAction()
{
// place all the logic you need here
// and every view placeholder assignment
$this->view->title = "My Title";
// if the logic requires a different markup
// simply redirect to another view
if (some_expression) {
$this->_forward('second');
}
}
public function secondAction()
{}
}
// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>
// 2. second.phtml
<h2><?php echo $this->title ?></h2>
As you can see in the second action there’s no logic – it’s only serving the role of a bridge between the views. Everything is setup into the first action, but the second action is doing the relation with the second.phtml view script.
Solution No. 2
However there’s another solution with only one action and two scripts which makes something like that shown on the diagram bellow:
The only thing to do is to add some code to your action:
<?php
class IndexController extends Zend_Controller_Action
{
public function firstAction()
{
// place all the logic you need here
// and every view placeholder assignment
$this->view->title = "My Title";
// if the logic requires a different markup
// simply redirect to another view
if (some_expression) {
echo $this->view->render('index/second.phtml');
$this->_helper->viewRenderer->setNoRender(true);
}
}
}
// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>
// 2. second.phtml
<h2><?php echo $this->title ?></h2> |
<?php
class IndexController extends Zend_Controller_Action
{
public function firstAction()
{
// place all the logic you need here
// and every view placeholder assignment
$this->view->title = "My Title";
// if the logic requires a different markup
// simply redirect to another view
if (some_expression) {
echo $this->view->render('index/second.phtml');
$this->_helper->viewRenderer->setNoRender(true);
}
}
}
// in the application/views/scripts/index
// 1. first.phtml
<h1><?php echo $this->title ?></h1>
// 2. second.phtml
<h2><?php echo $this->title ?></h2>
Note that you’ve to setNoRender on the view renderer once you render the other script!