In Zend Framework, a module can be defined as a unit of software that is portable and reusable and can be interconnected to other modules to construct a larger, complex application.
Modules are not new in Zend Framework, but with ZF2, there is a complete overhaul in the way modules are used in Zend Framework. With ZF2, modules can be shared across various systems, and they can be repackaged and distributed with relative ease. One of the other major changes coming into ZF2 is that even the main application is now converted into a module; that is, the application module.
Some of the key advantages of Zend Framework 2.0 modules are listed as follows:
◆ Self-contained, portable, reusable
◆ Dependency management
◆ Lightweight and fast
◆ Support for Phar packaging and Pyrus distribution
Zend Framework 2.0 – project folder structure
The folder layout of a ZF2 project is shown as follows:
Folder
name Description
config Used for managing
application configuration.
data Used as a temporary storage location for storing
application data including cache files, session files, logs, and indexes.
module Used to manage all application code.
module/Application This is the default application module that is provided with
ZendSkeletonApplication.
public Serves as an entry point to
the application; the website's document root points here. All web resources including CSS files, images, and JavaScripts are stored here.
vendor Used to manage common libraries
that are used by the application. Zend Framework
is also installed in this folder.
vendor/zendframework Zend Framework
2.0 is installed here.
Our next activity will be about creating a new Users module in Zend Framework 2.0. The Users module will be used for managing users including user registration, authentication, and so on. We will be making use ofZendSkeletonModule provided by Zend, shown as follows:
1. Navigate to the application's module folder:
$ cd /var/www/CommunicationApp/
$ cd module/
2. Clone ZendSkeletonModule into a desired module name, in this case it is Users:
$ git clone git://github.com/zendframework/ZendSkeletonModule.git Users
3. After the checkout is complete, the folder structure should look like the following screenshot:
4. Edit Module.php; this file will be located in the Users folder under modules (CommunicationApp/module/Users/module.php) and change the namespace to Users. Replace namespaceZendSkeletonModule; with namespace Users;.
5. The following folders can be removed because we will not be using them in our project:
* Users/src/ZendSkeletonModule
* Users/view/zend-skeleton-module
What just happened?
We have installed a skeleton module for Zend Framework; this is just an empty module, and we will need to extend this by creating custom controllers and views. In our next activity, we will focus on creating new controllers and views for this module.
MVC layer
The fundamental goal of any MVC Framework is to enable easier segregation of three layers of the MVC, namely, model, view, and controller. Before we get to the details of creating modules, let's quickly try to understand how these three layers work in an MVC Framework:
◆ Model: The model is a representation of data; the model also holds the business logic for various application transactions.
◆ View: The view contains the display logic that is used to display the various user interface elements in the web browser.
◆ Controller: The controller controls the application logic in any MVC application; all actions and events are handled at the controller layer. The controller layer serves as a communication interface between the model and the view by controlling the model state and also by representing the changes to the view. The controller also provides an entry point for accessing the application.
◆ In the new ZF2 MVC structure, all the models, views, and controllers are grouped by modules. Each module will have its own set of models, views, and controllers, and will share some components with other modules.
Zend Framework module – folder structure
The folder structure of Zend Framework 2.0 module has three vital components—the configurations, the module logic, and the views. The following table describes how contents in a module are organized:
Folder
name Description
config Used for managing
module configuration
src Contains all module source code,
including all controllers and models
view Used to store all the views used in the module
Now that we have created the module, our next step would be having our own controllers and views defined. In this section, we will create two simple views and will write a controller to switch between them:
1. Navigate to the module location:
$ cd /var/www/CommunicationApp/module/Users
2. Create the folder for controllers:
$ mkdir -p src/Users/Controller/
3. Create a new IndexController file, < ModuleName >/src/<ModuleName>/ Controller/:
$ cd src/Users/Controller/
$ vim IndexController.php
4. Add the following code to the IndexController file:
<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$view = new ViewModel(); return $view;
}
public function registerAction()
{
$view = new ViewModel();
$view->setTemplate('users/index/new-user'); return $view;
}
public function loginAction()
{
$view = new ViewModel();
$view->setTemplate('users/index/login'); return $view;
}
}
5. The preceding code will do the following actions; if the user visits the home page, the user is shown the default view; if the user arrives with an action register, the user is shown the new-user template; and if the user arrives with an action set to login, then the login template is rendered.
6. Now that we have created the controller, we will have to create necessary views to render for each of the controller actions.
7. Create the folder for views:
$ cd /var/www/CommunicationApp/module/Users
$ mkdir -p view/users/index/
8. Navigate to the views folder, <Module>/view/<module-name>/index:
$ cd view/users/index/
9. Create the following view files:
‰ index
‰ login
‰ new-user
1. For creating the view/users/index/index.phtml file, use the following code:
<h1>Welcome to Users Module</h1>
<a href="/users/index/login">Login</a> | <a href="/users/ index/register">New User Registration</a>
<h2> Login </h2>
<p> This page will hold the content for the login form </p>
<a href="/users"><< Back to Home</a>
3. For creating the view/users/index/new-user.phtml file, use the following code:
<h2> New User Registration </h2>
<p> This page will hold the content for the registration form </p>
<a href="/users"><< Back to Home</a>
What just happened?
We have now created a new controller and views for our new Zend Framework module; the module is still not in a shape to be tested. To make the module fully functional we will need to make changes to the module'sconfiguration, and also enable the module in the application's configuration.
Không có nhận xét nào:
Đăng nhận xét