Tuesday, 5 January 2016

Zend Framework 2 Service Manager

Zend Framework 2 Service Manager

Question: What is Service Manager?
Service Manager is component which have lot of services and used to set/get the service on the requirement. It implemented on "Service Locator" design Pattern.

Question: What is Service Locator?
The Service Locator is a service/object locator, which are used to retrieving other objects.

Question: What is Service Locator Pattern?
Service Locator Pattern is a design Pattern which is used to locate various services. this design pattern used by "Service Locator Service" to get the appropriate service.

Question: What are Service Factories?
Instead of getting actual object instance or a class name, We can also the ServiceManager to invoke a provided factory to get the object instance.

Question: How to create Service Factories?Giv an Example?
You can also create your own serverice factory using Zend\ServiceManager\FactoryInterface and Zend\ServiceManager\ServiceLocatorInterface.
See Example
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class MyNewFactory implements FactoryInterface
    public function createService(ServiceLocatorInterface $serviceLocator)
  /** Return the data  

    Return the data **/  

//Set the Factory
$serviceManager->setFactory('my-new-factory-name', new MyFactory()); 

//Get the Factory

Question: How to register an Object in Service manager?
$serviceManager->setService('new_obj_class1', new stdClass());
$serviceManager->setService('new_obj_class1'); // an instance of stdClass

Question: How to create aliases of any registered service?
$serviceManager->setService('my-object', $myObject);
$serviceManager->setAlias('my-alias1', 'my-object');
$serviceManager->setAlias('my-alias2', 'my-alias1')
/** my-object, my-alias1 and my-alias2 return same object **/

Question: What is use of setInvokableClass?
setInvokableClass allows you to tell the ServiceManager what class to instantiate when a particular service is requested.
$serviceManager->setInvokableClass('service-name', 'Fully\Qualified\ClassName');

Question: What is file location path Where we can set the configuration setting?

Question: What are various service manager setting which we can set?
We can set abstract_factories, aliases,factories,invokables, services and shared.

Question: Give an example of service manager config?
return array(
    'service_manager' => array(
        'abstract_factories' => array(            
        'aliases' => array(            
            'SomeModule\Model\User' => 'User',            
            'AdminUser' => 'User',            
            'SuperUser' => 'AdminUser',
        'factories' => array(            
            'User'     => 'SomeModule\Service\UserFactory',
            'UserForm' => function ($serviceManager) {
            $formObject = new SomeModule\Form\User();
            return $formObject;
        'invokables' => array(
            'UserInputFilter' => 'SomeModule\InputFilter\User',
        'services' => array(
            // Auth is the service names
            // Values are objects
            'Auth' => new SomeModule\Authentication\AuthenticationService(),
        'shared' => array(            
            'UserForm' => false,

No comments:

Post a Comment