Tag: view

  • Control Zend’s layout rendering and Content-Type specifications

    Control Zend’s layout rendering and Content-Type specifications

    In case you need to return content types other than the one set by server (Apache, etc.) or Zend itself use the following lines to quickly control Zend’s layout rendering and Content-Type specification.

    This is crucial for instance if your controller is required to return JSON or XML as part of a service request.

    // in your controller
    // 1. disable layout rendering
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    
    // 2. customize Content-Type
    $this->getResponse()->setHeader('Content-Type', 'application/json; charset=utf-8');
    
    // 3. echo' content as usual, e.g. JSON
    echo json_encode(array('foo' => 'bar'));