Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Zend\Mvc\Application Class Reference
Inheritance diagram for Zend\Mvc\Application:
Inheritance graph
[legend]

Public Member Functions

 __construct ($configuration, ServiceManager $serviceManager)
 
 getConfig ()
 
 bootstrap (array $listeners=array())
 
 getServiceManager ()
 
 getRequest ()
 
 getResponse ()
 
 getMvcEvent ()
 
 setEventManager (EventManagerInterface $eventManager)
 
 getEventManager ()
 
 run ()
 
 send ()
 

Static Public Member Functions

static init ($configuration=array())
 

Data Fields

const ERROR_CONTROLLER_CANNOT_DISPATCH = 'error-controller-cannot-dispatch'
 
const ERROR_CONTROLLER_NOT_FOUND = 'error-controller-not-found'
 
const ERROR_CONTROLLER_INVALID = 'error-controller-invalid'
 
const ERROR_EXCEPTION = 'error-exception'
 
const ERROR_ROUTER_NO_MATCH = 'error-router-no-match'
 

Protected Member Functions

 completeRequest (MvcEvent $event)
 

Protected Attributes

 $configuration = null
 
 $defaultListeners
 
 $event
 
 $events
 
 $request
 
 $response
 
 $serviceManager = null
 

Detailed Description

Main application class for invoking applications

Expects the user will provide a configured ServiceManager, configured with the following services:

The most common workflow is: $services = new Zend($servicesConfig); $app = new Application($appConfig, $services); $app->bootstrap(); $response = $app->run(); $response->send();

bootstrap() opts in to the default route, dispatch, and view listeners, sets up the MvcEvent, and triggers the bootstrap event. This can be omitted if you wish to setup your own listeners and/or workflow; alternately, you can simply extend the class to override such behavior.

Definition at line 46 of file Application.php.

Constructor & Destructor Documentation

Zend\Mvc\Application::__construct (   $configuration,
ServiceManager  $serviceManager 
)

Constructor

Parameters
mixed$configuration
ServiceManager$serviceManager

Definition at line 105 of file Application.php.

106  {
107  $this->configuration = $configuration;
108  $this->serviceManager = $serviceManager;
109 
110  $this->setEventManager($serviceManager->get('EventManager'));
111 
112  $this->request = $serviceManager->get('Request');
113  $this->response = $serviceManager->get('Response');
114  }
setEventManager(EventManagerInterface $eventManager)

Here is the call graph for this function:

Member Function Documentation

Zend\Mvc\Application::bootstrap ( array  $listeners = array())

Bootstrap the application

Defines and binds the MvcEvent, and passes it the request, response, and router. Attaches the ViewManager as a listener. Triggers the bootstrap event.

Parameters
array$listenersList of listeners to attach.
Returns
Application

Definition at line 136 of file Application.php.

137  {
140 
141  $listeners = array_unique(array_merge($this->defaultListeners, $listeners));
142 
143  foreach ($listeners as $listener) {
144  $events->attach($serviceManager->get($listener));
145  }
146 
147  // Setup MVC Event
148  $this->event = $event = new MvcEvent();
149  $event->setTarget($this);
150  $event->setApplication($this)
151  ->setRequest($this->getRequest())
152  ->setResponse($this->getResponse())
153  ->setRouter($serviceManager->get('Router'));
154 
155  // Trigger bootstrap events
157  return $this;
158  }
const EVENT_BOOTSTRAP
Definition: MvcEvent.php:23

Here is the call graph for this function:

Zend\Mvc\Application::completeRequest ( MvcEvent  $event)
protected

Complete the request

Triggers "render" and "finish" events, and returns response from event object.

Parameters
MvcEvent$event
Returns
Application

Definition at line 343 of file Application.php.

Referenced by Zend\Mvc\Application\run().

344  {
345  $events = $this->getEventManager();
346  $event->setTarget($this);
349  return $this;
350  }
const EVENT_RENDER
Definition: MvcEvent.php:27
const EVENT_FINISH
Definition: MvcEvent.php:26

Here is the call graph for this function:

Zend\Mvc\Application::getConfig ( )

Retrieve the application configuration

Returns
array|object

Definition at line 121 of file Application.php.

122  {
123  return $this->serviceManager->get('Config');
124  }
Zend\Mvc\Application::getEventManager ( )

Retrieve the event manager

Lazy-loads an EventManager instance if none registered.

Returns
EventManagerInterface

Implements Zend\EventManager\EventsCapableInterface.

Definition at line 223 of file Application.php.

Referenced by Zend\Mvc\Application\completeRequest(), Zend\Mvc\DispatchListener\marshallBadControllerEvent(), Zend\Mvc\DispatchListener\marshallControllerNotFoundEvent(), and Zend\Mvc\Application\run().

224  {
225  return $this->events;
226  }
Zend\Mvc\Application::getMvcEvent ( )

Get the MVC event instance

Returns
MvcEvent

Definition at line 195 of file Application.php.

Referenced by Zend\Mvc\Application\run().

196  {
197  return $this->event;
198  }
Zend\Mvc\Application::getRequest ( )

Get the request object

Returns

Implements Zend\Mvc\ApplicationInterface.

Definition at line 175 of file Application.php.

Referenced by Zend\Mvc\Application\bootstrap().

176  {
177  return $this->request;
178  }
Zend\Mvc\Application::getResponse ( )

Get the response object

Returns
ResponseInterface

Implements Zend\Mvc\ApplicationInterface.

Definition at line 185 of file Application.php.

Referenced by Zend\Mvc\Application\bootstrap(), and Zend\Mvc\Application\run().

186  {
187  return $this->response;
188  }
Zend\Mvc\Application::getServiceManager ( )

Retrieve the service manager

Returns
ServiceManager

Implements Zend\Mvc\ApplicationInterface.

Definition at line 165 of file Application.php.

166  {
167  return $this->serviceManager;
168  }
static Zend\Mvc\Application::init (   $configuration = array())
static

Static method for quick and easy initialization of the Application.

If you use this init() method, you cannot specify a service with the name of 'ApplicationConfig' in your service manager config. This name is reserved to hold the array from application.config.php.

The following services can only be overridden from application.config.php:

All other services are configured after module loading, thus can be overridden by modules.

Parameters
array$configuration
Returns
Application

Definition at line 247 of file Application.php.

Referenced by Zend\Test\PHPUnit\Controller\AbstractControllerTestCase\getApplication().

248  {
249  $smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
250  $listeners = isset($configuration['listeners']) ? $configuration['listeners'] : array();
251  $serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
252  $serviceManager->setService('ApplicationConfig', $configuration);
253  $serviceManager->get('ModuleManager')->loadModules();
254  return $serviceManager->get('Application')->bootstrap($listeners);
255  }
Zend\Mvc\Application::run ( )

Run the application

route(MvcEvent) Routes the request, and sets the RouteMatch object in the event. dispatch(MvcEvent) Dispatches a request, using the discovered RouteMatch and provided request. dispatch.error(MvcEvent) On errors (controller not found, action not supported, etc.), populates the event with information about the error type, discovered controller, and controller class (if known). Typically, a handler should return a populated Response object that can be returned immediately.

Returns
ResponseInterface

Implements Zend\Mvc\ApplicationInterface.

Definition at line 273 of file Application.php.

274  {
275  $events = $this->getEventManager();
276  $event = $this->getMvcEvent();
277 
278  // Define callback used to determine whether or not to short-circuit
279  $shortCircuit = function ($r) use ($event) {
280  if ($r instanceof ResponseInterface) {
281  return true;
282  }
283  if ($event->getError()) {
284  return true;
285  }
286  return false;
287  };
288 
289  // Trigger route event
290  $result = $events->trigger(MvcEvent::EVENT_ROUTE, $event, $shortCircuit);
291  if ($result->stopped()) {
292  $response = $result->last();
293  if ($response instanceof ResponseInterface) {
294  $event->setTarget($this);
295  $event->setResponse($response);
297  return $response;
298  }
299  if ($event->getError()) {
300  return $this->completeRequest($event);
301  }
302  return $event->getResponse();
303  }
304  if ($event->getError()) {
305  return $this->completeRequest($event);
306  }
307 
308  // Trigger dispatch event
309  $result = $events->trigger(MvcEvent::EVENT_DISPATCH, $event, $shortCircuit);
310 
311  // Complete response
312  $response = $result->last();
313  if ($response instanceof ResponseInterface) {
314  $event->setTarget($this);
315  $event->setResponse($response);
317  return $response;
318  }
319 
320  $response = $this->getResponse();
321  $event->setResponse($response);
322  $this->completeRequest($event);
323 
324  return $this;
325  }
completeRequest(MvcEvent $event)
const EVENT_DISPATCH
Definition: MvcEvent.php:24
const EVENT_FINISH
Definition: MvcEvent.php:26

Here is the call graph for this function:

Zend\Mvc\Application::send ( )
Deprecated:

Definition at line 330 of file Application.php.

331  {
332  }
Zend\Mvc\Application::setEventManager ( EventManagerInterface  $eventManager)

Set the event manager instance

Parameters
EventManagerInterface$eventManager
Returns
Application

Implements Zend\EventManager\EventManagerAwareInterface.

Definition at line 206 of file Application.php.

Referenced by Zend\Mvc\Application\__construct().

207  {
208  $eventManager->setIdentifiers(array(
209  __CLASS__,
210  get_class($this),
211  ));
212  $this->events = $eventManager;
213  return $this;
214  }

Here is the call graph for this function:

Field Documentation

Zend\Mvc\Application::$configuration = null
protected

Definition at line 59 of file Application.php.

Referenced by Zend\Mvc\Application\__construct(), and Zend\Mvc\Application\init().

Zend\Mvc\Application::$defaultListeners
protected
Initial value:
= array(
'RouteListener',
'DispatchListener',
'ViewManager',
'SendResponseListener',
)

Definition at line 66 of file Application.php.

Zend\Mvc\Application::$event
protected
Zend\Mvc\Application::$events
protected
Zend\Mvc\Application::$request
protected

Definition at line 87 of file Application.php.

Referenced by Zend\Mvc\Application\getRequest().

Zend\Mvc\Application::$response
protected

Definition at line 92 of file Application.php.

Referenced by Zend\Mvc\Application\getResponse(), and Zend\Mvc\Application\run().

Zend\Mvc\Application::$serviceManager = null
protected
const Zend\Mvc\Application::ERROR_CONTROLLER_CANNOT_DISPATCH = 'error-controller-cannot-dispatch'
const Zend\Mvc\Application::ERROR_CONTROLLER_INVALID = 'error-controller-invalid'
const Zend\Mvc\Application::ERROR_CONTROLLER_NOT_FOUND = 'error-controller-not-found'
const Zend\Mvc\Application::ERROR_EXCEPTION = 'error-exception'
const Zend\Mvc\Application::ERROR_ROUTER_NO_MATCH = 'error-router-no-match'

The documentation for this class was generated from the following file: