ZF2

Appendix: Build Your Own

Here are the steps required to work from the Zend Skeleton Application to the Starter Application (as at 29 July 2015).

  1. Download and install the Zend Skeleton Application according to its instructions
  2. Browse to the virtual host to check that it works
  3. Remove or un-configure any features that you do not require, for example caching or translation
  4. Add or configure any additional features that you require, for example configure logging
  5. Rename the Application module to Core (or another name), and update configuration so that it works
  6. Add a Mapper module including a minimum configuration (Module.php)
  7. Create the following directory structure in the Core/src directory (to mirror the Core namespace):
    Core     
        /Controller         
            /Plugin
        /Domain
            /iDomain
            /Proxy
            /Factory           
            /Collection             
                /iCollection             
                /Proxy
        /Service         
            /Event         
            /InputFilters     
        /View         
            /Helper      
        /Exception     
        /iMapper
    
  8. In the root of the Mapper module, create the following directory: src/Mapper/Exception
  9. Create the following files, or copy them from the Starter Application:
    Core/HelperTrait.php
    Core/Controller/AbstractController.php
    Core/iMapper/Mapper.php
    
    Core/Exception/Exception.php
    Core/Exception/Collection.php
    Core/Exception/Factory.php
    Core/Exception/InvalidInput.php
    Core/Exception/Proxy.php
    
    Core/Domain/AbstractDomain.php
    Core/Domain/DomainWatcher.php
    Core/Domain/Collection/AbstractCollection.php
    Core/Domain/Collection/iCollection/Collection.php
    Core/Domain/Collection/Proxy/AbstractCollectionProxy.php
    Core/Domain/Factory/AbstractFactory.php
    Core/Domain/Factory/FactoryLoader.php
    Core/Domain/iDomain/Domain.php
    Core/Domain/Proxy/AbstractProxy.php
    
    Core/Service/AbstractService.php
    Core/Service/ServiceLoader.php
    Core/Service/Event/AbstractListener
    
    Mapper/AbstractMapper.php
    Mapper/MapperLoader.php
    Mapper/Exception/Exception.php
    
  10. Modify the IndexController.php to extend from AbstractController.php. Browse the virtual host to check that it still works
  11. Configure the Core module to use abstract factories that create Domain object factories and Service Layer services:
    'service_manager' => array(     
        'abstract_factories' => array(
            'Core\Domain\Factory\FactoryLoader',         
            'Core\Service\ServiceLoader',
        ),
     ),
  12. Configure the Mapper module to use an abstract factory to create Mapper objects:
    'service_manager' => array(
        'abstract_factories' => array(
            'Mapper\MapperLoader',
        ),
    ),
  13. Browse to the virtual host again to check that it still works
Tags: 

PHP Starter Application

This is a starting point for applications with complex business logic. It is a PHP MVC skeleton with a Domain (Domain Model), Data Mapper and Service Layer. It uses Zend Framework 2 however may be ported to other frameworks quite easily. You are welcome to use it as the foundation for your next application.

The Starter Application includes:

  • A means of organising business logic (the Domain),
  • A flexible mechanism for database interaction (the Data Mapper),
  • A clean, clear API for client code (the Service Layer),
  • Factories for creating complex Domain objects,
  • An identity map to avoid duplicating Domain objects in memory,
  • Custom collections for Domain objects, and
  • Proxies for efficient loading of individual Domain objects and collections.

The Starter Application is aimed at intermediate-level developers who have ZF2 experience and are addressing problems which are too complex for transaction scripts or table modules. I am planning to maintain and improve both the code and this discussion, so please contribute any improvements by forking the code on github.

Overview

The Starter Application consists of two modules: Core and Mapper. All codes lives in Core, with the exception of the Data Mapper which is declared in Core and implemented in Mapper.

The presentation layer of the Starter Application (i.e. views and controllers), is straightforward. All the interesting work takes place in the model which includes the Domain, Data Mapper and Service Layer.

The MVC Model

Getting Started

To start building your own application you can:

  1. Install the Starter Application using the method described here
  2. Read each of the topics that follow, exploring the installed code as you go
  3. Follow the steps in Using the Starter Application