In this article, we will develop a Zend Framework2 web application which demonstrates how to create new modules, controllers, actions and views in a Zend Framework2 application.
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 “Starting up a Zend Framework 2 application“.
1. Setup the application “Zf2StudentApp”
Please follow the steps explained in the article titled “Starting up a Zend Framework 2 application” and ensure that you are getting the screen as shown in the Figure 1, below :
2. Create a module namely “Student” in the application Zf2StudentApp
$cd /var/www/Zf2StudentApp $sudo php ../zftool.phar create module student
3. Creating a controller namely “StudentController” in the module “Student“
- Create a file namely StudentController.php in the directory Zf2StudentApp/Module/Student/src/Student/Controller
- Add the given below php code to the file StudentController.php
<?php namespace Student\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class StudentController extends AbstractActionController{ public function indexAction(){ } }
- The above “StudentController” has an action namely “index” action
4. Create a view for “index” action of the “StudentController”
- Create a file namely “index.phtml” in the directory Zf2StudentApp/module/Student/view/student/student
- Add the given below code to the file index.phtml
<h1>This is Module : Student</h1>
5. Configure the router by editing 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( 'template_path_stack'=>array( 'student'=>__DIR__ . '/../view' ) ) );
6. Access the module by entering the url “http://zf2studentapp.local/student” or “http://zf2studentapp.local/student/index
7. Download Full Code
The source for this application can be downloaded fromĀ here.
The application repository is availableĀ here
8. What is next?
A continuation to this article is available at “Zend Framework2 – Getting data in View layer from action controller using ViewModel”

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