Pop\Mvc\Controller::send PHP Method

send() public method

Finalize the request and send the response.
public send ( integer $code = 200, array $headers = null ) : void
$code integer
$headers array
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();
    }

Usage Example

Ejemplo n.º 1
0
 public function testSendException()
 {
     $this->setExpectedException('Pop\\Mvc\\Exception');
     $c = new Controller();
     $c->send();
 }