3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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\ServiceLocatorInterface
;
21 use Zend\ServiceManager\ServiceManager
;
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\FormInterface $form, $redirect = null, $redirectToUrl = false)
33 * @method bool|array|\Zend\Http\Response filePostRedirectGet(\Zend\Form\FormInterface $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()
43 * @method \Zend\View\Model\ConsoleModel createConsoleNotFoundModel()
44 * @method \Zend\View\Model\ViewModel createHttpNotFoundModel(Response $response)
46 abstract class AbstractController
implements
48 EventManagerAwareInterface
,
49 InjectApplicationEventInterface
67 * @var ServiceLocatorInterface
69 protected $serviceLocator;
77 * @var EventManagerInterface
82 * @var null|string|string[]
84 protected $eventIdentifier;
92 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->setName(MvcEvent
::EVENT_DISPATCH
);
112 $e->setRequest($request);
113 $e->setResponse($response);
114 $e->setTarget($this);
116 $result = $this->getEventManager()->triggerEventUntil(function ($test) {
117 return ($test instanceof Response
);
120 if ($result->stopped()) {
121 return $result->last();
124 return $e->getResult();
132 public function getRequest()
134 if (!$this->request
) {
135 $this->request
= new HttpRequest();
138 return $this->request
;
142 * Get response object
146 public function getResponse()
148 if (!$this->response
) {
149 $this->response
= new HttpResponse();
152 return $this->response
;
156 * Set the event manager instance used by this context
158 * @param EventManagerInterface $events
159 * @return AbstractController
161 public function setEventManager(EventManagerInterface
$events)
163 $className = get_class($this);
165 $nsPos = strpos($className, '\\') ?
: 0;
166 $events->setIdentifiers(array_merge(
170 substr($className, 0, $nsPos)
172 array_values(class_implements($className)),
173 (array) $this->eventIdentifier
176 $this->events
= $events;
177 $this->attachDefaultListeners();
183 * Retrieve the event manager
185 * Lazy-loads an EventManager instance if none registered.
187 * @return EventManagerInterface
189 public function getEventManager()
191 if (!$this->events
) {
192 $this->setEventManager(new EventManager());
195 return $this->events
;
199 * Set an event to use during dispatch
201 * By default, will re-cast to MvcEvent if another event type is provided.
206 public function setEvent(Event
$e)
208 if (!$e instanceof MvcEvent
) {
209 $eventParams = $e->getParams();
211 $e->setParams($eventParams);
218 * Get the attached event
220 * Will create a new MvcEvent if none provided.
224 public function getEvent()
227 $this->setEvent(new MvcEvent());
234 * Set serviceManager instance
236 * @param ServiceLocatorInterface $serviceLocator
239 public function setServiceLocator(ServiceLocatorInterface
$serviceLocator)
241 $this->serviceLocator
= $serviceLocator;
245 * Retrieve serviceManager instance
247 * @return ServiceLocatorInterface
249 public function getServiceLocator()
251 trigger_error(sprintf(
252 'You are retrieving the service locator from within the class %s. Please be aware that '
253 . 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
254 . 'with the ServiceLocatorAwareInitializer. You will need to update your class to accept '
255 . 'all dependencies at creation, either via constructor arguments or setters, and use '
256 . 'a factory to perform the injections.',
258 ), E_USER_DEPRECATED
);
260 return $this->serviceLocator
;
266 * @return PluginManager
268 public function getPluginManager()
270 if (!$this->plugins
) {
271 $this->setPluginManager(new PluginManager(new ServiceManager()));
274 $this->plugins
->setController($this);
275 return $this->plugins
;
281 * @param PluginManager $plugins
282 * @return AbstractController
284 public function setPluginManager(PluginManager
$plugins)
286 $this->plugins
= $plugins;
287 $this->plugins
->setController($this);
293 * Get plugin instance
295 * @param string $name Name of plugin to return
296 * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
299 public function plugin($name, array $options = null)
301 return $this->getPluginManager()->get($name, $options);
305 * Method overloading: return/call plugins
307 * If the plugin is a functor, call it, passing the parameters provided.
308 * Otherwise, return the plugin instance.
310 * @param string $method
311 * @param array $params
314 public function __call($method, $params)
316 $plugin = $this->plugin($method);
317 if (is_callable($plugin)) {
318 return call_user_func_array($plugin, $params);
325 * Register the default events for this controller
329 protected function attachDefaultListeners()
331 $events = $this->getEventManager();
332 $events->attach(MvcEvent
::EVENT_DISPATCH
, [$this, 'onDispatch']);
336 * Transform an "action" token into a method name
338 * @param string $action
341 public static function getMethodFromAction($action)
343 $method = str_replace(['.', '-', '_'], ' ', $action);
344 $method = ucwords($method);
345 $method = str_replace(' ', '', $method);
346 $method = lcfirst($method);