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…]

Virtual Host

What is virtual host?

According to Wikipedia: Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently.

Setup for Windows Platform

At first let me tell you my setup, I have windows in my C: drive and I have installed XAMPP in my D: drive. For windows platform (with XAMPP) you need to do 3 things. Thing 1: go to your XAMPP installation drive then in my case it is D:, and open the httpd-xampp.conf file. In my case it is located here D:\xampp\apache\conf\extra. Thing 2: Add these lines of code in the httpd-xampp.conf

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs/project/path"
	ServerName yourhostname.local
</VirtualHost>

NOTE: One thing to note here is, the default host (localhost) must be the last host defined in this file, Otherwise worlds will be at WAR!!!. So your final look of the httpd-vhosts.conf file should be something similar to this

NameVirtualHost *:80

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs/project/path"
	ServerName yourhostname.local
</VirtualHost>

<VirtualHost *:80>
	DocumentRoot "D:/xampp/htdocs"
	ServerName localhost
</VirtualHost>

NOTE: If you have installed xampp in your windows root directory then you might be unable to save the edited file. In that case open the notepad in admin mood (Run as administrator) by right clicking on its icon. Then open the file from within notepad by selecting File > Open... Thing 3: You need to register your custom domain (in my case it is yourhostname.local) in the window’s host file. As I have my windows in C: drive so my host file locates here C:\Windows\System32\drivers\etc. Now read again what I said in my last NOTE point and then open the host file using notepad by following that instruction. In that host file, add this line, DON’T delete any existing lines in that file (yeah that’s common sense!).

127.0.0.1        yourhostname.local

and Save the file. and One More Thing: OK I admit it is now becoming 4 things, so Just restart your apache server very quickly by selecting XAMPP control panel. If it fails to restart for some unknown reason just restart your PC.

Setup for Linux Platform

For Linux, it is quite straight forward. Step 1: Add a sites entry in this directory /etc/apache2/sites-available. If you don’t know how just copy the default as yourhostname.local

$ sudo cp default yourhostname.local

Step 2: Open your newly created site file (In this case it is yourhostname.local) with any linux editor, I’ll use VIM.

$ sudo vi yourhostname.local

Then add the following lines of code then save and close.

<VirtualHost *:80>
    ServerName yourhostname.local
    DocumentRoot /home/username/yourproject/path
   <Directory /var/username/yourproject/path>
       DirectoryIndex index.php
       AllowOverride All
       Order allow,deny
       allow from all
   </Directory>
</VirtualHost>

Step 3: Register the host in the /etc/host file

$ sudo vi /etc/host

Step 4: Enable the new host with this command

$sudo a2ensite yourhostname.local

Step 5: Restart apache server. Command for this is

$ sudo /etc/init.d/apache2 restart

Or

$ sudo service apache reload

That’s it for now. Thanks.