Different Layouts for different Controller Actions in Zend Framework2

February 16, 2013

In this article, we will see how to specify different layouts for different controller actions in Zend Framework2.

This article belongs the series “Web application development with Zend Framework 2“.

This article is a continuation to the article titled “Zend Framework2 – Getting data in View layer from action controller using ViewModel“.

1. Setup the application

Please follow the steps explained in the article titled ”Zend Framework2 – Getting data in View layer from action controller using ViewModel” to ensure that you are getting a screen as shown in the Figure 1, below :

Application Screenshot before applying action specific layout

Figure 1 : Application Screenshot before applying action specific layout

From the above figure, it is clear the “Student” module has obtained a layout common to application. In the below given steps we are adding a new layout namely “student_layout” by which the layout of this module will be changed as shown in the figure 2.

2. Update the file Zf2StudentApp/module/Student/config/module.config.php

return array(


        /* Defining layout file for index action of StudentController */
        'template_map' => array(
            'layout/student_layout' => __DIR__ . '/../view/layout/student_layout.phtml'

            'student'=>__DIR__ . '/../view'

3. Create the layout file Zf2StudentApp/module/Student/view/layout/student_layout.phtml

<h1 style="text-align: center;">
    Student Module
    echo $this->content;

4. Update the file Zf2StudentApp/module/Student/src/Student/Controller/StudentController.php

namespace Student\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class StudentController extends AbstractActionController{
    public function indexAction(){
        return new ViewModel(array(
                    "name"=>"A Mitra",
                    "name"=>"Mukherjee P S",
                    "name"=>"Rani Mathew",
                    "department"=>"Computer Science",
                    "name"=>"Rakesh Krishna",
                    "department"=>"Computer Science",
                    "name"=>"Faisal Ahmed",

5. Screenshot of the application

Screenshot of the application in execution

Figure 2 : Screenshot of the application in execution

6. Download application

The source for this application can be downloaded from here.

The application repository is available here

7. What is next?

A continuation to this article is available at “Setup model and table classes to fetch data from MySQL database using TableGateway in Zend Framework2

How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner