eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController::buildResponse PHP Method

buildResponse() protected method

Build the response so that depending on settings it's cacheable.
protected buildResponse ( string | null $etag = null, DateTime $lastModified = null ) : Response
$etag string | null
$lastModified DateTime
return Symfony\Component\HttpFoundation\Response
    protected function buildResponse($etag = null, DateTime $lastModified = null)
    {
        $request = $this->getRequest();
        $response = new Response();
        if ($this->getParameter('content.view_cache') === true) {
            $response->setPublic();
            if ($etag !== null) {
                $response->setEtag($etag);
            }
            if ($this->getParameter('content.ttl_cache') === true) {
                $response->setSharedMaxAge($this->getParameter('content.default_ttl'));
            }
            // Make the response vary against X-User-Hash header ensures that an HTTP
            // reverse proxy caches the different possible variations of the
            // response as it can depend on user role for instance.
            if ($request->headers->has('X-User-Hash')) {
                $response->setVary('X-User-Hash');
            }
            if ($lastModified != null) {
                $response->setLastModified($lastModified);
            }
        }
        return $response;
    }

Usage Example

 /**
  * Build the response so that depending on settings it's cacheable
  *
  * @param string|null $etag
  * @param \DateTime|null $lastModified
  *
  * @return \Symfony\Component\HttpFoundation\Response
  * @see \eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController::buildResponse
  */
 protected function buildResponse($etag = null, DateTime $lastModified = null)
 {
     if ('POST' === $this->getRequest()->getMethod()) {
         $response = new Response();
         $response->setPrivate();
         return $response;
     } else {
         return parent::buildResponse($etag, $lastModified);
     }
 }