Zf2 Configuration

To get a hold on Zend Framework, one must understand its configuration. How Zend Framework looks for certain service when they gets called. Zend Framework stores every information of its services within an array. One must wonder why Zend Framework people choose array to store information regarding services. My guess is because of its O notation. An array has an O notation of O(1) which is a constant time when it comes to retrieving a value. Which in our case will be in micro second or less. So that may clear people mind why Zend Framework choose arrays.

So now we come back to our topic Zend Framework configurations. As in any framework there are certain keywords which are reserved for the framework alone and if we play with them the Framework will most likely will malfunction. So lets check some of the reserved array key keywords which as Zend Framework uses. And they are namely as follows:

  • Modules array key modules
  • Service Manager array key service_manager
  • Router array key router
  • Translator array key translator
  • Controllers array key controllers
  • View Manager array key view_manager
  • View Helpers array key view_helpers
  • Console array key console

There you go the most used reserved keywords of Zend Framework. Now I'll try to explain what are their purpose according to my findings.


Modules key is a the most simple array key in Zend Framework. It just holds the information off all the modules which your application going to have. There is no limit for modules. You can have a single modules or hundreds of modules depending on the complexity of your we application.

Service Manager

Service Manager key holds information of all your services that you want to use in you application. What information exactly. Mostly it holds information regarding your abstract classes, forms, models and all your business related services(classes). Service manager in itself contains two keys namely:
  • Invokables
  • Factories
Now this is the most important key which you will encounter in Zend Framework.


Invokables are the the services which don't require any configuration what so ever. In other word which are free from dependencies. Now whats the use of it. Simple to have one object through out the application. No, you don't agree. So lets have a test.

Open your file in /your_base_path/modules/Application/config/module.config.php

insert the following code in service_manager key.


return array(

   'service_manager' => array(
        'invokables' => array(
            'myTestService' => 'Application\Service\Test',

Now create a file name Test.php in /your_base_path/modules/Application/src/Application/Service/Test.php. In that paste in the below code:


namespace Application\Service;

class Test{
    protected static $static = 0;
    public function __construct() {
    public function getStaticValue(){
        return self::$static;

Finally to test it. Put in the below code in the IndexController of the Application module indexAction

    public function indexAction(){
        $testInvokables = $this->getServiceLocator()->get('myTestService');
        $testInvokables = $this->getServiceLocator()->get('myTestService');
        $testObject = new \Application\Service\Test();
        $testObject = new \Application\Service\Test();
        return new ViewModel();


Factories are the services which requires dependencies to be injected in them. Services resides within factories also have one single instance throughout the application. The dependencies may vary from simple array injection to many objects insertion at the same time. Factories services needs to have at least one dependency to be injected in it. We will check this out in when we will explore ZfcUser.


Router contains all the information regarding the uri's of your application.


Controllers key contains all the information regarding your application controllers. Controllers also have invokable/factories key in them. If your controller requires any objects which needs to be inserted you've to create it from a factories key.

View Manager

View Manager contains all information regarding views in Zend Framework. Where to look up for a *.phtml file. Load which file when certain module/controller/action got hit.

View Helpers

View Helpers are services which helps in creating widgets.


Console key contains all information of routes which can be triggered via command line.