Different Layouts for different Controller Actions in Zend Framework2

February 16, 2013
By

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


<?php
return array(
    'controllers'=>array(
        'invokables'=>array(
            'Student\Controller\Student'=>'Student\Controller\StudentController'
        )
    ),

    'router'=>array(
        'routes'=>array(
            'student'=>array(
                'type'=>'segment',
                'options'=>array(
                    'route'=>'/student[/:action][/:id]',
                    'constraints'=>array(
                        'action'=>'[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'=>'[0-9]+'
                    ),
                    'defaults'=>array(
                        'controller'=>'Student\Controller\Student',
                        'action'=>'index'
                    )
                )
            )
        )
    ),

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

        'template_path_stack'=>array(
            'student'=>__DIR__ . '/../view'
        )
    )
);


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

<html>
<body>
<h1 style="text-align: center;">
    Student Module
</h1>
<?php
    echo $this->content;
?>
</body>
</html>

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


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

class StudentController extends AbstractActionController{
    public function indexAction(){
        $this->layout("layout/student_layout");
        return new ViewModel(array(
            'students'=>array(
                array(
                    "name"=>"A Mitra",
                    "department"=>"Physics",
                    "marks"=>77
                ),
                array(
                    "name"=>"Mukherjee P S",
                    "department"=>"Physics",
                    "marks"=>89
                ),
                array(
                    "name"=>"Rani Mathew",
                    "department"=>"Computer Science",
                    "marks"=>91
                ),
                array(
                    "name"=>"Rakesh Krishna",
                    "department"=>"Computer Science",
                    "marks"=>72
                ),
                array(
                    "name"=>"Faisal Ahmed",
                    "department"=>"ComputerScience",
                    "marks"=>93
                )
            )
        ));
    }
}



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
store4js.blogspot.com


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