Zend Framework2 – Getting data in View layer from action controller using ViewModel

February 9, 2013
By

In this article, we will see how to get data in view layer ( presentation layer ) from action controller using ViewModel class.

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

For this, we will continue with the application “Zf2StudentApp” developed in the article titled “Zend Framework2 – Creating module, controller, actions, views and routing“.


1. Setup the application “Zf2StudentApp”

Please follow the steps explained in the article titled “Zend Framework2 – Creating module, controller, actions, views and routing” and ensure that you are getting the screen shown in the Figure 1, below :

Screenshot of the application with Student Module
Figure 1 : Screenshot of the application with Student Module

2. Update the indexAction of the StudentController in 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(
            '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
                )
            )
        ));
    }
}


3. Update the index.phtml file


<h1>
    Students List
</h1>

<table border="1">
    <thead>
        <tr>
            <th>No</th>
            <th>Name</th>
            <th>Department</th>
            <th>Marks</th>
        <tr>
    </thead>

<?php
$i=1;
foreach($students as $student){
    echo "<tr>";
    echo "<td>" . $i++ . "</td>";
    echo "<td>" . $student["name"] . "</td>";
    echo "<td>" . $student["department"] . "</td>";
    echo "<td>" . $student["marks"] . "</td>";
    echo "</tr>";
}
?>
</table>



4. Screenshot of the application

Showing Students List fetched from Action Controller
Figure 2 : Showing Students List fetched from Action Controller

5. Download application

The source for this application can be downloaded from here.

The application repository is available here


6. What is next?

A continuation to this article is available at “Different Layouts for different Controller Actions 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