Thứ Năm, 24 tháng 4, 2014

Zend Framework 2.0 – modules [Building Your First Zend Framework Application]

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 completoverhaul in the way modules are used in Zend Framework. With ZF2, modules can be shared across various systems, and thecan be repackaged and distributed with relative ease. One of the other major changes coming into ZF2 is thaeven 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 temporarstorage 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 pointo the application; the website's  documenroot points here. All web resources including CSS files,  images, and JavaScripts arstored here.
vendor                   Used to managcommon 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.       Navigatto 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 foldercan 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 textend this by creating custom controllers and views. In our next activity, we will focus on creating necontrollers and viewfor 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. Beforwgeto the details of creating modules, let's quickly trto understand how these three layerwork 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 viecontains 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 viebcontrolling the model state and also brepresenting the changes to the view. The controller also provides an entry poinfor accessing the application.


        In the new ZF2 MVstructure, 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 hocontents 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 tstore all the views used in the module

Now that we have created the module, our nexstep would be having our own controllers and views defined. In this section, we will create two simple views and will write a controller tswitch between them:

1.       Navigatto the module location:
$ cd /var/www/CommunicationApp/module/Users

2.       Create the folder for controllers:
$ mkdir -p src/Users/Controller/

3.       Create a neIndexController 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 setlogin, then the login template is rendered.
6.       Now thawe have created the controllerwe will havto create necessary viewtrender for each of the controller actions.
7.       Create the folder for views:
$ cd /var/www/CommunicationApp/module/Users
$ mkdir -p view/users/index/

8.       Navigatto the viewfolder<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>


2.       For creating the view/users/index/login.phtml file, use the following code:

<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 necontroller and viewfor our neZend 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