/** * Finalize the request and send the response. * * @param int $code * @param array $headers * @throws \Pop\Mvc\Exception * @return void */ public function send($code = 200, array $headers = null) { if (null === $this->view) { throw new Exception('The view object is not defined.'); } if (!$this->view instanceof View) { throw new Exception('The view object is not an instance of Pop\\Mvc\\View.'); } if (null !== $this->project->logger()) { $this->project->log("Response [" . $code . "]", time()); } $this->response->setCode($code); if (null !== $headers) { foreach ($headers as $name => $value) { $this->response->setHeader($name, $value); } } // Trigger any dispatch events, then send the response if (null !== $this->project->getEventManager()->get('dispatch')) { $this->project->log('[Event] Dispatch', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch', array('controller' => $this)); $this->response->setBody($this->view->render(true)); if (null !== $this->project->getEventManager()->get('dispatch.send')) { $this->project->log('[Event] Dispatch Send', time(), \Pop\Log\Logger::NOTICE); } $this->project->getEventManager()->trigger('dispatch.send', array('controller' => $this)); $this->response->send(); }