Zend Framework 2: Services

Services.

There are various ways through which the services can be configured into the Service Manager.

Service Manager Configuration –

Configuration requires a “service_manager” key at the top level of your configuration, with any of the following sub-keys:

  • abstract_factories, which should be an array of abstract factory class names.
  • aliases, which should be an associative array of alias name/target name pairs (where the target name may also be an alias).
  • factories, an array of service name/factory class name pairs. The factories should be either classes implementing “Zend\ServiceManager\FactoryInterface” or invokable classes. If you are using PHP configuration files, you may provide any PHP callable as the factory.
  • invokables, an array of service name/class name pairs. The class name should be class that may be directly instantiated without any constructor arguments.
  • services, an array of service name/object pairs. Clearly, this will only work with PHP configuration.
  • shared, an array of service name/boolean pairs, indicating whether or not a service should be shared. By default, theServiceManager assumes all services are shared, but you may specify a boolean false value here to indicate a new instance should be returned.
  • initializers, .
  • allow_override, .

a. abstract_factories: an array of service name/class name pairs. The class name should be a class that may be directly instantiated without any constructor arguments. Mostly Controllers are defined here.

/* __ModuleName__/config/module.config.php */

return array(

‘controllers’ => array (

‘invokables’ => array (

‘__ModuleName__\Controller\User’ => ‘__ModuleName__\Controller\UserController’,

)

)

);

Reference:

http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.quick-start.html

http://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/

Zend Framework 2: Service Manager

1. Service Manager.

a. Service Locator.

Service Locator is a design pattern which is used in processes involved in obtaining a service. This pattern uses a central registry known as the “service locator”, which on request returns the information necessary to perform a certain task.

Advantages:

  • The “service locator” can act as a simple run-time linker. This allows code to be added at run-time without re-compiling the application, and in some cases without having to even restart it.
  • Large sections of a library or application can be completely separated. The only link between them becomes the registry.

Disadvantages:

  • The registry hides the class’ dependencies, causing run-time errors instead of compile-time errors when dependencies are missing.
  • The registry makes the code more difficult to maintain (opposed to using Dependency Injection), because it becomes unclear when you would be introducing a breaking change.

In a nutshell:

  • Service Locator in Zend Framework 2 can be used for looking up a Service and using it, at run-time.

Best practice:

  • Avoid using Service Locator, instead use Dependency Injection.

Usage:

In Zend Framework 2 –

Service Locator is implemented by Service Manager “Zend\ServiceManager” component.

The services are defined and registered to the Service Manager. To access these services at run-time, first the access to Service Manager is required. To do that, the Service Locator is used.

ie. The Service Locator is needed (not always) to access the Service Manager. Using Service Manager, the defined Services can be accessed.

i. Inside Controller –

$serviceLocator = $this->getServiceLocator();

ii. Inside Module.php –

namespace __ModuleName__;
use Zend\Mvc\MvcEvent;

class Module {

public function onBootstrap(MvcEvent $e) {

$sm = $e->getApplication()->getServiceManager();
}
}

iii. Inside Controller Plugin –

$serviceLocator = $this->getController()->getServiceLocator();

Reference:

http://framework.zend.com/manual/2.2/en/modules/zend.service-manager.intro.html

http://en.wikipedia.org/wiki/Service_locator_pattern

https://en.wikipedia.org/wiki/Dependency_injection

http://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/

Zend Framework 2 Certification – Exam Topics

Service Manager

  • Service Locator
  • Service Factories
  • Abstract Service Factories
  • Aliasing
  • Configuration
  • Invokables
  • Shared
  • Initalizers
  • Plugin managers

Event Manager

  • Aggregate Listeners
  • Shared event Manager & event id
  • Event Propagation & short circuit
  • Attach
  • Trigger
  • EM inside the Framework
  • Attach
  • Trigger

MVC

  • MVC Dispatch Flow
  • Dispatch
  • App Config & Merged Config
  • Using the shared Event Mgr
  • Routing
  • Controller
  • View
  • Bootstrap
  • Escape for Output/Cross site Scripting

Module Manager

  • Concept of Module
  • ModuleManager Events
  • Bootstrap sequence
  • Module Class
  • Best Practices
  • Understanding Config’s layers

Authentication and Authorization

  • Secure Authentication
  • Authentication Storage Usage
  • Authentication Adapter
  • Manage an ACL
  • Manage RBAC

Forms

  • Binding Objects to Forms
  • Form Element manager & form build
  • Elements
  • Input Filters & Validators
  • View rendering/helpers
  • Fieldsets (reusability)
  • Forms
  • Hydrator Integration with forms

Filtering and Validation

  • Zend/Input/Filter/Input Theory
  • Attaching Validators & Filtering
  • Standard Filters
  • Secure File Upload
  • Filtering & Validation

Database

  • Parameters quoting
  • SQL Abstraction (query builder)
  • Table Data, Abstract Table
  • Adapter
  • HydratingResultSet

Utility

  • Zend Log
  • Mail
  • Stdlib
  • Session
  • Pagination

Internationalization

  • Locales
  • Domains & Adaptors
  • Built-in i18n View Helpers

Web Services

  • HttpClient
  • Web Service clients
  • RESTful services
  • Soap Services
  • Core Web Services Component

Performance

  • Caching configuration
  • View template resolution
  • Autoloaders performance

Security

  • Session Security
  • Security Best Practices
  • Cryptography
  • Secure Password Storing
  • Public key encryption
  • Encryption

Reference:

http://www.zend.com/en/services/certification/framework-2/