Zend Framework 2: A Bit of Insight

January 19, 2015



The current version of the Zend Framework 2 is RC2 although,weekly fixes and updates have been going on, currently the only tutorial left to start with the Zend Framework 2 is from "Rob Allen", the author whose pdf  "QuickStart with Zend Framework 1" gave me a good understanding of the framework, i somehow carried the notion that with Zend framework 1 knowledge,that  i could easily adapt to Zend Framework 2, to be quite frank ZF2 is a total learning curve on its own, as we get to see addition of ServiceManager, EventManager, Dependency Injector and the best part of all, Module manager, i'm am going to be very honest with you, am not a hardcore PHP Developer, but i know enough to get me by, and Zend framework 2 is a bit powerful but the learning curve is anything but easy but once you understand the Zend Skeleton 2 in the Github you can get a pretty decent understanding of bootstrapping and routing.

 

BASIC UNDERSTANDING

Modules and Configurations

ZF2 is centered towards Modules, so your application will have to be organized with each of its modules,e.g My Admin backend would be in a "AdminApplication" module and my frontend n a "FrontendApplication". each Modules has its specific Configuration , Auto-loader and required Classes to be loaded if needed; The Zend ModuleManager will look for a Module.php in every module folder you create.  the method getConfig retrieves the Configuration for the Module, this configuration is simply an array where the controller is specified and the view path to be used, you should note that all ths configurations will be used by the ServiceManager to pass them to the relevant components.

Application Configurations

if you are familiar with the Yii Framework there's a main config responsible for loading classes and passing information that can be used later on in the application, Zend also allows configuration and also all configuration passed in Application,or Modules will be merged together before being processed. the "config/application.config.php" it  provides configuration for the ServiceManager,ModuleManager.

Controllers and ViewModels

pretty much the same as ZF1 ,except all controllers extends the AbstractActionController and we can use an Object called the ViewModel a bit similar to Microsoft ViewBag and Yii, we can return the name of a view to be loaded and also pass variables to used by the view script, returning and we can also return an array with data, a ViewModel will be created behind the scene saving keystrokes.

Using ServiceManager to configure the database credentials and inject into the
controller

Now this part really confused me for a while because after going the tutorial for the 5th time, i decided it was far saner to research the operations under the hood, because i really had no idea how the Db Configuration was been seen and what was responsible for instantiating it, it felt like voodoo and trust me in Web Programming voodoo is the last thing you need, an in-depth knowledge of internal operation never hurts anyone.

  • How ZF2 loads the database in Rob Allen's Guide

The Configuration in the "config/autoload/global.php" holds a Global Configuration which holds the DB Credentials, in an array key called "db", in the next line is the ServiceManager which calls a Factory(a class responsible for constructing an Object) as denoted in the array "factories", it takes an array, now what happens here is that it creates a "Zend\Db\Adapter\AdapterServiceFactory" and stores it in a key called "Zend/Db/Adapter", this key can be changed to anything i used "Database" to store the Adapter Object.

What happens here is that it loads the AdapterServiceFactory class which implement the FactoryInterface and implements the method "createService" and saves it with the "Zend/Db/Adapter"

 

public function getServiceConfiguration()
{
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new AlbumTable($dbAdapter);
return $table;
},
),

The code creates an AlbumTable which is stored in the key  "Album\Model\AlbumTable", which will be used in the controller so we need it instantiated and doing this in the controller is actually a bad practice, so we provide a Callback which takes the ServiceManager as the parameter, by calling the ServiceManager::get it actually loads the AdapterServiceFactory and triggers the createService Method. which contains the following code, to see it check the ZF2 Library

public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Configuration');
return new Adapter($config['db']);
}

The ServiceLocation loads the Configuration which contains all of our config and uses the db key holding the Database credentials and returns an instantiated object of a Db/Adapter which is passed in our class(AlbumTable) that extends the AbstractTableGateway.

ZF2 is quite the framework however there are certain concepts which needs certain attention:

ServiceManager

Modulemanager

EventManager

Configurations

NamespacingClosures , Anonymous Function and PSR-0

The rest are quite easy to pick along i just decided to focus on the aspect that is key to ZF2, hopefully i hope to get acquainted with ZF2 and provide more information on it, my advise is take a look at the source and get to understand ZF2.

 

 NOTE: Please feel free to point out any error i might have made.

 

 


Post Comment