Default Services¶
The default and recommended way to write Zend Framework applications uses a set of services defined in the
Zend\Mvc\Service
namespace. This chapter details what each of those services are, the classes they represent,
and the configuration options available.
ServiceManager¶
This is the one service class referenced directly in the bootstrapping. It provides the following:
Invokable services
DispatchListener
, mapping toZend\Mvc\DispatchListener
.Request
, mapping toZend\Http\PhpEnvironment\Request
.Response
, mapping toZend\Http\PhpEnvironment\Response
.RouteListener
, mapping toZend\Mvc\RouteListener
.ViewManager
, mapping toZend\Mvc\View\ViewManager
.
Factories
Application
, mapping toZend\Mvc\Service\ApplicationFactory
.Configuration
, mapping toZend\Mvc\Service\ConfigFactory
. Internally, this pulls theModuleManager
service, and calls itsloadModules()
method, and retrieves the merged configuration from the module event. As such, this service contains the entire, merged application configuration.ControllerLoader
, mapping toZend\Mvc\Service\ControllerLoaderFactory
. Internally, this pulls theConfiguration
service, and, if it contains acontroller
key, inspects that forclasses
andfactories
subkeys. These are used to configure a scoped service manager container, from which controllers will be retrieved.Additionally, the scoped container is configured to use the
Di
service as an abstract service factory – effectively allowing you to fall back to DI in order to retrieve your controllers. If you want to useZend\Di
to retrieve your controllers, you must white-list them in your DI configuration under theallowed_controllers
key (otherwise, they will just be ignored).Finally, if the loaded controller is
Pluggable
, an initializer will inject it with theControllerPluginBroker
service.ControllerPluginBroker
, mapping toZend\Mvc\Service\ControllerPluginBrokerFactory
. This instantiates theZend\Mvc\Controller\PluginBroker
instance, passing it theControllerPluginLoader
service as well as the service manager instance.ControllerPluginLoader
, mapping toZend\Mvc\Service\ControllerPluginLoaderFactory
. This grabs theConfiguration
service, and looks for acontroller
key with amap
subkey. If found, this value is passed to the constructor ofZend\Mvc\Controller\PluginLoader
(otherwise, an empty array is passed).DependencyInjector
, mapping toZend\Mvc\Service\DiFactory
. This pulls theConfiguration
service, and looks for a “di” key; if found, that value is used to configure a newZend\Di\Di
instance. Additionally, theDi
instance is used to seed aZend\ServiceManager\Di\DiAbstractServiceFactory
instance which is then attached to the service manager as an abstract factory – effectively enabling DI as a fallback for providing services.EventManager
, mapping toZend\Mvc\Service\EventManagerFactory
. This factory composes a static reference to aSharedEventManager
, which is injected in a newEventManager
instance. This service is not shared by default, allowing the ability to have anEventManager
per service, with a sharedSharedEventManager
injected in each.ModuleManager
, mapping toZend\Mvc\Service\ModuleManagerFactory
.This is perhaps the most complex factory in the MVC stack. It expects that an
ApplicationConfiguration
service has been injected, with keys formodule_listener_options
andmodules
; see the quick start for samples.It instantiates an instance of
Zend\ModuleManager\Listener\DefaultListenerAggregate
, using the “module_listener_options” retrieved. It also instantiates an instance ofZend\ModuleManager\Listener\ServiceListener
, providing it the service manager.Next, it retrieves the
EventManager
service, and attaches the above listeners.It instantiates a
Zend\ModuleManager\ModuleEvent
instance, setting the “ServiceManager” parameter to the service manager object.Finally, it instantiates a
Zend\ModuleManager\ModuleManager
instance, and injects theEventManager
andModuleEvent
.Router
, mapping toZend\Mvc\Service\RouterFactory
. This grabs theConfiguration
service, and pulls from therouter
key, passing it toZend\Mvc\Router\Http\TreeRouteStack::factory
in order to get a configured router instance.ViewFeedRenderer
, mapping toZend\Mvc\Service\ViewFeedRendererFactory
, which simply returns aZend\View\Renderer\FeedRenderer
instance.ViewFeedStrategy
, mapping toZend\Mvc\Service\ViewFeedStrategyFactory
. This instantiates aZend\View\Strategy\FeedStrategy
instance with theViewFeedRenderer
service.ViewJsonRenderer
, mapping toZend\Mvc\Service\ViewJsonRendererFactory
, which simply returns aZend\View\Renderer\JsonRenderer
instance.ViewJsonStrategy
, mapping toZend\Mvc\Service\ViewJsonStrategyFactory
. This instantiates aZend\View\Strategy\JsonStrategy
instance with theViewJsonRenderer
service.
Aliases
Config
, mapping to theConfiguration
service.Di
, mapping to theDependencyInjector
service.Zend\EventManager\EventManagerInterface
, mapping to theEventManager
service. This is mainly to ensure that when falling through to DI, classes are still injected via theServiceManager
.Zend\Mvc\Controller\PluginBroker
, mapping to theControllerPluginBroker
service. This is mainly to ensure that when falling through to DI, classes are still injected via theServiceManager
.Zend\Mvc\Controller\PluginLoader
, mapping to theControllerPluginLoader
service. This is mainly to ensure that when falling through to DI, classes are still injected via theServiceManager
.
Additionally, two initializers are registered. Initializers are run on created instances, and may be used to
further configure them. The two initializers the ServiceManagerConfig
class creates and registers do the
following:
- For objects that implement
Zend\EventManager\EventManagerAwareInterface
, theEventManager
service will be retrieved and injected. This service is not shared, though each instance it creates is injected with a shared instance ofSharedEventManager
. - For objects that implement
Zend\ServiceManager\ServiceManagerAwareInterface
, theServiceManager
will inject itself into the object.
Finally, the ServiceManager
registers itself as the ServiceManager
service, and aliases itself to the class
names Zend\ServiceManager\ServiceManagerInterface
and Zend\ServiceManager\ServiceManager
.
ViewManager¶
The View layer within Zend\Mvc
consists of a large number of collaborators and event listeners. As such,
Zend\Mvc\View\ViewManager
was created to handle creation of the various objects, as well as wiring them
together and establishing event listeners.
The ViewManager
itself is an event listener on the bootstrap
event. It retrieves the ServiceManager
from the Application
object, as well as its composed EventManager
.
Configuration for all members of the ViewManager
fall under the view_manager
configuration key, and expect
values as noted below. The following services are created and managed by the ViewManager
:
ViewHelperLoader
, representing and aliased toZend\View\HelperLoader
. If ahelper_map
subkey is provided, its value will be used as a map to seed the helper loader.ViewHelperBroker
, representing and aliased toZend\View\HelperBroker
. It is seeded with theViewHelperLoader
service, as well as theServiceManager
itself.The
Router
service is retrieved, and injected into theUrl
helper.If the
base_path
key is present, it is used to inject theBasePath
view helper; otherwise, theRequest
service is retrieved, and the value of itsgetBasePath()
method is used.If the
doctype
key is present, it will be used to set the value of theDoctype
view helper.ViewTemplateMapResolver
, representing and aliased toZend\View\Resolver\TemplateMapResolver
. If atemplate_map
key is present, it will be used to seed the template map.ViewTemplatePathStack
, representing and aliased toZend\View\Resolver\TemplatePathStack
. If atemplate_path_stack
key is prsent, it will be used to seed the stack.ViewResolver
, representing and aliased toZend\View\Resolver\AggregateResolver
andZend\View\Resolver\ResolverInterface
. It is seeded with theViewTemplateMapResolver
andViewTemplatePathStack
services as resolvers.ViewRenderer
, representing and aliased toZend\View\Renderer\PhpRenderer
andZend\View\Renderer\RendererInterface
. It is seeded with theViewResolver
andViewHelperBroker
services. Additionally, theViewModel
helper gets seeded with theViewModel
as its root (layout) model.ViewPhpRendererStrategy
, representing and aliased toZend\View\Strategy\PhpRendererStrategy
. It gets seeded with theViewRenderer
service.View
, representing and aliased toZend\View\View
. It gets seeded with theEventManager
service, and attaches theViewPhpRendererStrategy
as an aggregate listener.DefaultRenderingStrategy
, representing and aliased toZend\Mvc\View\DefaultRenderingStrategy
. If thelayout
key is prsent, it is used to seed the strategy’s layout template. It is seeded with theView
service.ExceptionStrategy
, representing and aliased toZend\Mvc\View\ExceptionStrategy
. If thedislay_exceptions
orexception_template
keys are present, they are usd to configure the strategy.RouteNotFoundStrategy
, representing and aliased toZend\Mvc\View\RouteNotFoundStrategy
and404Stategy
. If thedisplay_not_found_reason
ornot_found_template
keys are present, they are used to configure the strategy.ViewModel
. In this case, no service is registered; theViewModel
is simply retrieved from theMvcEvent
and injected with the layout template name. template
The ViewManager
also creates several other listeners, but does not expose them as services; these include
Zend\Mvc\View\CreateViewModelListener
, Zend\Mvc\View\InjectTemplateListener
, and
Zend\Mvc\View\InjectViewModelListener
. These, along with RouteNotFoundStrategy
, ExceptionStrategy
, and
DefaultRenderingStrategy
are attached as listeners either to the application EventManager
instance or the
SharedEventManager
instance.
Finally, if you have a strategies
key in your configuration, the ViewManager
will loop over these and
attach them in order to the View
service as listeners, at a priority of 100 (allowing them to execute before
the DefaultRenderingStrategy
).
Application Configuration Options¶
The following options may be used to provide initial configuration for the ServiceManager
, ModuleManager
,
and Application
instances, allowing them to then find and aggregate the configuration used for the
Configuration
service, which is intended for configuring all other objects in the system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <?php
return array(
// This should be an array of module namespaces used in the application.
'modules' => array(
),
// These are various options for the listeners attached to the ModuleManager
'module_listener_options' => array(
// This should be an array of paths in which modules reside.
// If a string key is provided, the listener will consider that a module
// namespace, the value of that key the specific path to that module's
// Module class.
'module_paths' => array(
),
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively overide configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
),
// Whether or not to enable a configuration cache.
// If enabled, the merged configuration will be cached and used in
// subsequent requests.
'config_cache_enabled' => $booleanValue,
// The key used to create the configuration cache file name.
'config_cache_key' => $stringKey,
// The path in which to cache merged configuration.
'cache_dir' => $stringPath,
),
// Initial configuration with which to seed the ServiceManager.
// Should be compatible with Zend\ServiceManager\Config.
'service_manager' => array(
),
);
|
Default Configuration Options¶
The following options are available when using the default services configured by the
ServiceManagerConfig
and ViewManager
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <?php
return array(
// The following are used to configure controller or controller plugin loading
'controller' => array(
// Map of controller "name" to class
// This should be used if you do not need to inject any dependencies
// in your controller
'classes' => array(
),
// Map of controller "name" to factory for creating controller instance
// You may provide either the class name of a factory, or a PHP callback.
'factories' => array(
),
// Map of controller plugin names to their classes
'map' => array(
),
),
// The following is used to configure a Zend\Di\Di instance.
// The array should be in a format that Zend\Di\Config can understand.
'di' => array(
),
// Configuration for the Router service
// Can contain any router configuration, but typically will always define
// the routes for the application. See the router documentation for details
// on route configuration.
'router' => array(
'routes' => array(
),
),
// ViewManager configuration
'view_manager' => array(
// Defined helpers.
// Typically helper name/helper class pairs. Can contain values without keys
// that refer to either Traversable classes or Zend\Loader\PluginClassLoader
// instances as well.
'helper_map' => array(
'foo' => 'My\Helper\Foo', // name/class pair
'Zend\Form\View\HelperLoader', // additional helper loader to seed
),
// Base URL path to the application
'base_path' => $stringBasePath,
// Doctype with which to seed the Doctype helper
'doctype' => $doctypeHelperConstantString, // e.g. HTML5, XHTML1
// TemplateMapResolver configuration
// template/path pairs
'template_map' => array(
),
// TemplatePathStack configuration
// module/view script path pairs
'template_path_stack' => array(
),
// Layout template name
'layout' => $layoutTemplateName, // e.g., 'layout/layout'
// ExceptionStrategy configuration
'display_exceptions' => $bool, // display exceptions in template
'exception_template' => $stringTemplateName, // e.g. 'error'
// RouteNotFoundStrategy configuration
'display_not_found_reason' => $bool, // display 404 reason in template
'not_found_template' => $stringTemplateName, // e.g. '404'
// Additional strategies to attach
// These should be class names or service names of View strategy classes
// that act as ListenerAggregates. They will be attached at priority 100,
// in the order registered.
'strategies' => array(
'ViewJsonStrategy', // register JSON renderer strategy
'ViewFeedStrategy', // register Feed renderer strategy
),
),
);
|