Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Bind Zend Action with Non-Default View

DZone's Guide to

Bind Zend Action with Non-Default View

· Web Dev Zone
Free Resource

Prove impact and reduce risk when rolling out new features. Optimizely Full Stack helps you experiment in any application.

Action – View

You may know that every controller’s action in Zend Framework has to be bind to a view. Normally you can disable the view for a specific action, but how about “forking” an action to render different views?!

In example when some _GET parameter is set redirect to another view? That’s rather strange and perhaps there’s a clear and yet full MVC solution! First of all you can setup all of the view variables simply in the “parent” action.
<?php

class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->title = 'This is the default action!';
}
}

Than the view file will contain something like:

// index/index.phtml
<h1><?php echo $this->title ?></h1>
Than whenever you have the given _GET set you can _forward to another action (perhaps with no code at all) and only a different view. Thus you don’t setup twice the view variables.
<?php

class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->title = 'This is the default action!';

if (isset($_GET['my_param'])) {
$this->_forward('another');
}
}

public function anotherAction()
{}
}
And there are two views practically for one action:
// index/index.phtml
<h1><?php echo $this->title ?></h1>

// index/another.phtml
<h2><?php echo $this->title ?></h2>

Related posts:

  1. Escaping strings in a Zend Framework view. Prevent unclosed tags!
  2. Download Files with Zend Framework
  3. Setting Up a Specific Layout for a Zend Action

With SDKs for all major client and server side platforms, you can experiment on any platform with Optimizely Full Stack.

Topics:

Published at DZone with permission of Stoimen Popov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}