public function setEventManager(EventManagerInterface $events) { // events property defined in AbstractController $this->events = $events; parent::setEventManager($events); // Register the listener and callback method with a priority of 10 $events->attach('dispatch', array($this, 'checkOptions'), 10); }
/** * Set the event manager instance used by this context * * @param EventManagerInterface $events * * @return $this|\Zend\Mvc\Controller\AbstractController */ public function setEventManager(EventManagerInterface $events) { parent::setEventManager($events); $this->events = $events; // Register a listener at high priority $events->attach('dispatch', function (MvcEvent $e) { if (!isset($this->entityClass)) { throw new \Exception('Controller is not properly set up. "entityClass" is missing', 500); } //Check is method is NOT allowed if (!in_array($e->getRequest()->getMethod(), $this->allowedOptions)) { /** @var Response $response */ $response = $e->getResponse(); // Return 405 $response->setStatusCode(405); //Indicate what _is_ allowed $response->getHeaders()->addHeaderLine('Allow', implode(',', $this->allowedOptions)); return $response; } return true; }, 10); return $this; }
public function setEventManager(EventManagerInterface $events) { parent::setEventManager($events); $this->events->attach('dispatch', [$this, 'checkOptions'], 10); }