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

viewLocation() public method

Response will be cached with HttpCache validation model (Etag).
Deprecation: Since 6.0.0. Viewing locations is now done with ViewContent.
public viewLocation ( integer $locationId, string $viewType, boolean $layout = false, array $params = [] ) : Response
$locationId integer
$viewType string
$layout boolean
$params array
return Symfony\Component\HttpFoundation\Response
    public function viewLocation($locationId, $viewType, $layout = false, array $params = array())
    {
        trigger_error("ViewController::viewLocation() is deprecated since kernel 6.0.0, and will be removed in the future.\n" . 'Use ViewController::viewAction() instead.', E_USER_DEPRECATED);
        $this->performAccessChecks();
        $response = $this->buildResponse();
        try {
            if (isset($params['location']) && $params['location'] instanceof Location) {
                $location = $params['location'];
            } else {
                $location = $this->getRepository()->getLocationService()->loadLocation($locationId);
                if ($location->invisible) {
                    throw new NotFoundHttpException("Location #{$locationId} cannot be displayed as it is flagged as invisible.");
                }
            }
            $response->headers->set('X-Location-Id', $locationId);
            $response->setContent($this->renderLocation($location, $viewType, $layout, $params));
            return $response;
        } catch (UnauthorizedException $e) {
            throw new AccessDeniedException();
        } catch (NotFoundException $e) {
            throw new NotFoundHttpException($e->getMessage(), $e);
        } catch (NotFoundHttpException $e) {
            throw $e;
        } catch (Exception $e) {
            return $this->handleViewException($response, $params, $e, $viewType, null, $locationId);
        }
    }

Usage Example

 /**
  * Main action for viewing content through a location in the repository.
  * Response will be cached with HttpCache validation model (Etag)
  *
  * it is possible to change the ttl of the cache by setting a global Variable
  *
  * e.g. in twig template with a twig function which set the global variable
  *
  *  $GLOBALS['EZ_TWIG_HTTP_CACHE_TTL'] = 0; => disable cache
  *  $GLOBALS['EZ_TWIG_HTTP_CACHE_TTL'] = 3; => 3 s
  *
  * @param int $locationId
  * @param string $viewType
  * @param boolean $layout
  * @param array $params
  *
  * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
  * @throws \Exception
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function viewLocation($locationId, $viewType, $layout = false, array $params = array())
 {
     // run the original ViewController from Ez
     // if all is ok we get a Response object otherwise an exception
     $response = parent::viewLocation($locationId, $viewType, $layout, $params);
     if ($response instanceof Response) {
         if (isset($GLOBALS['CJW_HTTP_CACHE_TTL'])) {
             $ttlFromTemplate = (int) $GLOBALS['CJW_HTTP_CACHE_TTL'];
             $response->setTtl($ttlFromTemplate);
             if ($ttlFromTemplate == 0) {
                 $response->setPrivate();
             }
             //echo __METHOD__ . " get http_cache ttl from tpl: $ttlFromTemplate<br>";
             //$response->headers->set( 'X-Cache-Ttl', $ttlFromTemplate );
         }
     }
     return $response;
 }