Getting started with Zend 2

Zend 2 was released in September 2012 offering significant changes to the original Zend 1 released 5 years before. While the Zend documentation is considerbly improved for this latest release, it is still a lot to digest in one go and some of the trickier new concepts can be difficult to understand. This article aims to give you an overview of these changes and shows you how to create a 'hello world' module as an introduction to getting started with the framework.

Throughout the article I'll provide links to some of the best resources for explaining some of the more complicated concepts in detail where you want to dig deeper.

About Zend...

For those of you new to Zend... it is an open-source PHP framework for developing web applications, and is one of the most widely adopted alongside other big names like Symfony and Code Igniter. It implements a Model View Controller (MVC) architecture to seperate code, and is flexible in that you can use specific modules, or adopt the full framework. The goal is to provide you with a starting structure for your application and a wealth of core code for developing common features like forms and access management. 

So what's new?

The Core

Zend Framework 2 is still considered a Model View Controller (MVC) framework, though the core has changed considerably. The developers have had a strong focus on increasing flexibility and decoupling code - to achive this composition is encouraged over inheritence so Dependecy Injection (DI) is encouraged throughout the framework. This is evident in some of the core new features such as the Service and Event Manger, which we'll discuss further below. 

ZF1 relied a lot on the singleton pattern and features like the registry to pass objects around the application, ZF2 takes an Aspect Orientated Design (AOD) approach whereby you can hook into events to attach listeners that trigger specific actions. 

Namespaces

Now using PHP 5.3 ZF2 takes advantage of namespaces. Each module (we'll get to that), now has it's own namespace for better code separation. 

Modules

One of ZF2's key goals was to reduce code depenencies so that functionality is more easily transferred between applications in standalone form. This is achieved in ZF2 using 'Modules'. Modules are basically a way of grouping code, an examples could be a 'user', 'forum' or 'contact' module. 

Zend has an area on their site dedicated to modules shared by other users, you can view them on modules.zendframework.com.

Event Manager

ZF2 is called an 'event-driven framework', and uses the event manager as part of its core, but it's also available for you to use when building your applications. The event manager is the used for managing 'events' (such as 'dispatch'), which are triggered by the application. You can attach 'listeners' to these events so that when they are triggered you can execute additional code (in Aspect Orientated programming, this additional code is referred to as the 'aspect', i.e. a common feature typically re-used across different classes such as 'logging').

As a practical example, here's a snippet from a module 'CSActionLayouts' that aims to set the layout for different controllers or actions when a module is bootstrapped. To achieve this, it attaches a listener on the controller abstract and 'dispatch' event so that when this is triggered, it will run the code to set the layout. This saves us having to to use the setLayout method in all our controllers or actions. 

class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $sem = $e->getApplication()->getEventManager()->getSharedManager();

        $sem->attach( 'Zend\Mvc\Controller\AbstractController', 'dispatch', function($e)
        {
// Set the layout using the module config file...
}

You can view the full source on bitbucket. Michael Gallego provides a good introduction to the event manager and why it is useful. 

Services Manager

The Service Manager implements the 'service locator pattern' used for retrieiving objects. It is essentially a registry of the shared objects / services you want to make available in your application so that they can be loaded anywhere in the application in a re-usable fashion.

Retrieving services is pretty straight forward, but configuring the service manager can cause some confusion as there's 7 different ways it can be done! 

MasterZendFramework.com provide a good guide to the configuration options with a practical example of how it would be used. 

Get Started - Installation

Zend 2 is available using Composer, Pyrus, or you can get the files directly from GIT. View the download options here

Zend also provide a guide (complete with vhost setup information) on how to setup their skeleton application in their documentation.

Create a Hello World Module

So far this article should have given you a gentle introduction to the key concepts of Zend 2 with plenty of resources to further reading. With the Zend skeleton application installed, in the next section let's have a go at creating a hello world module.

Create a module

Additional Resources

Matthew Weier O'Phinney introduction to Zend 2: https://www.youtube.com/watch?v=NsOrT1R6IQ0 

ZF2 Architecture: http://www.zend.com/topics/MVC-architecture-ZF2.pdf 

Sign Up

NEXT: Create a module in zend 2

This article runs you through the steps of creating a new module in Zend Framework 2.

comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.