Whuhay
Whuhay

Reputation: 1

What is a service in ZF2

I have searched a lot to understand what is a service and why and when should we use them? What is the best way to create a service? What is its difference with plugin?

I have read resources from here and here.

Upvotes: 1

Views: 81

Answers (2)

AlexP
AlexP

Reputation: 9857

What is a service?

A 'service' is any information in your application (often a PHP object instance) that is registered with the service manager.

The service manager acts as a central repository for all your classes; abstracting their creation using 'service factories'. This will allow the information to be requested in any area of the application using one or more 'service names'.

The terminology used is not specific to ZF2 but rather the Service Locator and Dependancy Injection design patterns.

Why use a service?

  • Registering a service name or alias

You remove the need to reference/create a object by its class name; instead you request it by it's service name. If any any point my application's requirements for FooService were to be different I could swap the default FooServiceFactory for a new, perhaps very different service by simply updating the factory that is registered to that name.

There is only one location where your class are created (a service factory). Consider a class that has many constructor arguments, if you were to add an additional argument it would mean finding all instances where you have created the object using new and adding the new argument.

  • Dependency Injection

In complex applications the arguments needed to create a service can themselves be complex. We would call these dependencies. We can model these dependencies as services themselves and use the service manager to request and 'inject' them into the required service.

Consider an EmailService, it has many complex dependencies, all of them can be requested from the service manager, each having their own factories.

Example email service factory:

class EmailServiceFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new EmailService(
            $serviceLocator->get('Email\Config\EmailConfig'),
            $serviceLocator->get('Email\Entity\Repository\EmailRepository'),
            $serviceLocator->get('Message\Entity\Repository\MessageStatusRepository'),
            $serviceLocator->get('Email\Service\EmailTransportService'),
            $serviceLocator->get('Email\Service\EmailTemplateService')
        );
    }
}

ZF2

  • Factories

There are many different ways you can register your service with the service locator (essentially different factories). Which 'type' of factory you use will normally depend on the service's dependencies. These are explained in detail in the documentation.

  • Plugin Managers

Plugin managers, which are specialized service managers used to manage objects that are of a related type, such as view helpers, controller plugins, controllers, etc.

The framework logically separates different types of services, although fundamentally they are all created by the service manager, it makes considerably easier to manage and create similar service with their own service manager. Some examples include the FormElementManager, ControllerPluginManager and ViewHelperPluginManager.

Upvotes: 3

goten4
goten4

Reputation: 81

A service can be any object that will be instanciated by a service locator.

Here are common services :

  • Controllers
  • Controllers plugins
  • View helpers

If you're using Zend\Db you may also have a UserTableGateway service, to manage your users persistence. Then you will have to provide a factory to the service locator that will instanciate the class and provide the dependencies (table name, adapter, etc).

Upvotes: 0

Related Questions