3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller
;
12 use Zend\EventManager\EventInterface
as Event
;
13 use Zend\EventManager\EventManager
;
14 use Zend\EventManager\EventManagerAwareInterface
;
15 use Zend\EventManager\EventManagerInterface
;
16 use Zend\Http\PhpEnvironment\Response
as HttpResponse
;
17 use Zend\Http\Request
as HttpRequest
;
18 use Zend\Mvc\InjectApplicationEventInterface
;
19 use Zend\Mvc\MvcEvent
;
20 use Zend\ServiceManager\ServiceLocatorAwareInterface
;
21 use Zend\ServiceManager\ServiceLocatorInterface
;
22 use Zend\Stdlib\DispatchableInterface
as Dispatchable
;
23 use Zend\Stdlib\RequestInterface
as Request
;
24 use Zend\Stdlib\ResponseInterface
as Response
;
29 * Convenience methods for pre-built plugins (@see __call):
31 * @method \Zend\View\Model\ModelInterface acceptableViewModelSelector(array $matchAgainst = null, bool $returnDefault = true, \Zend\Http\Header\Accept\FieldValuePart\AbstractFieldValuePart $resultReference = null)
32 * @method bool|array|\Zend\Http\Response fileprg(\Zend\Form\Form $form, $redirect = null, $redirectToUrl = false)
33 * @method bool|array|\Zend\Http\Response filePostRedirectGet(\Zend\Form\Form $form, $redirect = null, $redirectToUrl = false)
34 * @method \Zend\Mvc\Controller\Plugin\FlashMessenger flashMessenger()
35 * @method \Zend\Mvc\Controller\Plugin\Forward forward()
36 * @method mixed|null identity()
37 * @method \Zend\Mvc\Controller\Plugin\Layout|\Zend\View\Model\ModelInterface layout(string $template = null)
38 * @method \Zend\Mvc\Controller\Plugin\Params|mixed params(string $param = null, mixed $default = null)
39 * @method \Zend\Http\Response|array prg(string $redirect = null, bool $redirectToUrl = false)
40 * @method \Zend\Http\Response|array postRedirectGet(string $redirect = null, bool $redirectToUrl = false)
41 * @method \Zend\Mvc\Controller\Plugin\Redirect redirect()
42 * @method \Zend\Mvc\Controller\Plugin\Url url()
44 abstract class AbstractController
implements
46 EventManagerAwareInterface
,
47 InjectApplicationEventInterface
,
48 ServiceLocatorAwareInterface
71 * @var EventManagerInterface
76 * @var ServiceLocatorInterface
78 protected $serviceLocator;
83 protected $eventIdentifier;
91 abstract public function onDispatch(MvcEvent
$e);
97 * @events dispatch.pre, dispatch.post
98 * @param Request $request
99 * @param null|Response $response
100 * @return Response|mixed
102 public function dispatch(Request
$request, Response
$response = null)
104 $this->request
= $request;
106 $response = new HttpResponse();
108 $this->response
= $response;
110 $e = $this->getEvent();
111 $e->setRequest($request)
112 ->setResponse($response)
115 $result = $this->getEventManager()->trigger(MvcEvent
::EVENT_DISPATCH
, $e, function ($test) {
116 return ($test instanceof Response
);
119 if ($result->stopped()) {
120 return $result->last();
123 return $e->getResult();
131 public function getRequest()
133 if (!$this->request
) {
134 $this->request
= new HttpRequest();
137 return $this->request
;
141 * Get response object
145 public function getResponse()
147 if (!$this->response
) {
148 $this->response
= new HttpResponse();
151 return $this->response
;
155 * Set the event manager instance used by this context
157 * @param EventManagerInterface $events
158 * @return AbstractController
160 public function setEventManager(EventManagerInterface
$events)
162 $events->setIdentifiers(array(
163 'Zend\Stdlib\DispatchableInterface',
166 $this->eventIdentifier
,
167 substr(get_class($this), 0, strpos(get_class($this), '\\'))
169 $this->events
= $events;
170 $this->attachDefaultListeners();
176 * Retrieve the event manager
178 * Lazy-loads an EventManager instance if none registered.
180 * @return EventManagerInterface
182 public function getEventManager()
184 if (!$this->events
) {
185 $this->setEventManager(new EventManager());
188 return $this->events
;
192 * Set an event to use during dispatch
194 * By default, will re-cast to MvcEvent if another event type is provided.
199 public function setEvent(Event
$e)
201 if (!$e instanceof MvcEvent
) {
202 $eventParams = $e->getParams();
204 $e->setParams($eventParams);
211 * Get the attached event
213 * Will create a new MvcEvent if none provided.
217 public function getEvent()
220 $this->setEvent(new MvcEvent());
227 * Set serviceManager instance
229 * @param ServiceLocatorInterface $serviceLocator
232 public function setServiceLocator(ServiceLocatorInterface
$serviceLocator)
234 $this->serviceLocator
= $serviceLocator;
238 * Retrieve serviceManager instance
240 * @return ServiceLocatorInterface
242 public function getServiceLocator()
244 return $this->serviceLocator
;
250 * @return PluginManager
252 public function getPluginManager()
254 if (!$this->plugins
) {
255 $this->setPluginManager(new PluginManager());
258 $this->plugins
->setController($this);
259 return $this->plugins
;
265 * @param PluginManager $plugins
266 * @return AbstractController
268 public function setPluginManager(PluginManager
$plugins)
270 $this->plugins
= $plugins;
271 $this->plugins
->setController($this);
277 * Get plugin instance
279 * @param string $name Name of plugin to return
280 * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
283 public function plugin($name, array $options = null)
285 return $this->getPluginManager()->get($name, $options);
289 * Method overloading: return/call plugins
291 * If the plugin is a functor, call it, passing the parameters provided.
292 * Otherwise, return the plugin instance.
294 * @param string $method
295 * @param array $params
298 public function __call($method, $params)
300 $plugin = $this->plugin($method);
301 if (is_callable($plugin)) {
302 return call_user_func_array($plugin, $params);
309 * Register the default events for this controller
313 protected function attachDefaultListeners()
315 $events = $this->getEventManager();
316 $events->attach(MvcEvent
::EVENT_DISPATCH
, array($this, 'onDispatch'));
320 * Transform an "action" token into a method name
322 * @param string $action
325 public static function getMethodFromAction($action)
327 $method = str_replace(array('.', '-', '_'), ' ', $action);
328 $method = ucwords($method);
329 $method = str_replace(' ', '', $method);
330 $method = lcfirst($method);