Zend Framework 2

Step 1 – Composer

I wrote this step in a separate post which is here.

Step 2 – The Skeleton Application

In order to build our application, we will start with the ZendSkeletonApplication available on github. Use Composer (http://getcomposer.org) to create a new project from scratch with Zend Framework:

php composer.phar create-project --repository-url="https://packages.zendframework.com" -s dev zendframework/skeleton-application path/to/install

NOTE: replace the path/to/install with your intended project path (eg. demo_app).

After executing this command, it will download the skeleton application with all its dependency like the Zend Framework 2. It might take some time depending on your internet speed. keep patience!

NOTE: After installing the skeleton application, you will see one more composer.phar inside it. From now on we will be using this composer.phar

Step 3 – Virtual Host

See how to create a virtual host from here.

Step 4 – Zend Framework Tool (ZFTool)

ZFTool is a utility module for maintaining modular Zend Framework 2 applications. It runs from the command line and can be installed as ZF2 module or as PHAR (see below). This tool gives you the ability to:

  • create a ZF2 project, installing a skeleton application;
  • create a new module inside an existing ZF2 application;
  • get the list of all the modules installed inside an application;
  • get the configuration file of a ZF2 application;
  • install the ZF2 library choosing a specific version.

Requirements:

Zend Framework 2.0.0 or later.

PHP 5.3.3 or later.

Console access to the application being maintained (shell, command prompt)

Installation using Composer

php composer require zendframework/zftool:dev-master

This command will install the ZFTool inside the vendor/zendframework/zftool directory. Now we can use this tool with this command

php vendor/zendframework/zftool/zf.php

OR you can just download the PHAR package and use it. In that case you don’t need to install it with composer.

Suppose we want to see the list of modules in our project, we can do this by this command:

php vendor/zendframework/zftool/zf.php modules

You can read more about ZFTool usage commands from this link https://github.com/zendframework/ZFTool

Step 4 – Module

Zend Framework 2 uses a module system to organize application specific code within each module. Inside the skeleton application there is a directory called module. Inside the module directory we see a directory called Application (module), the skeleton application by default creates this module.

Let’s create a module named Album using the ZFTool. To do that we need to execute this command:

php vendor/zendframework/zftool/zf.php create module Alubm

This will create a Album module with all the required configuration inside the module directory.

Create a Controller

Let’s create a Controller named Album using the ZFTool. To do that we need to execute this command: zf.php create controller {controller-name} {module-name}

php vendor/zendframework/zftool/zf.php create controller Alubm Album

Now configure this controller in the module.config.php with this code:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
        ),
    ),

    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/][:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
        ),
    ),
);

To monitor your application’s performance you can install Zend Developer Tools. Read more about this tool from here.

Step 5 – Doctrine

What is Doctrine?
Doctrine 2 is an object-relational mapper (ORM) for PHP 5.3.3+ that provides transparent persistence for PHP objects. It uses the Data Mapper pattern at the heart, aiming for a complete separation of your domain/business logic from the persistence in a relational database management system.

What are Entities?
Entities are PHP Objects that can be identified over many requests by a unique identifier or primary key. These classes don’t need to extend any abstract base class or interface. An entity class must not be final or contain final methods. Additionally it must not implement clone nor wakeup or do so safely.

Doctrine ORM Module

DoctrineORMModule integrates Doctrine 2 ORM with Zend Framework 2 quickly and easily.

Installation

To install this module you need to execute this following command using composer

php composer.phar require doctrine/doctrine-orm-module
# (When asked for a version, type `0.*`)

Then add DoctrineModule and DoctrineORMModule to your config/application.config.php and create directory data/DoctrineORMModule/Proxy and make sure your application has write access to it.

[To be continued…]

Leave a comment