You are here

class AliasController in Mini site 8

Class AliasController.

Controller to deliver a single aliased asset. Non-aliased assets and non-html documents are never delivered through this controller.

@package Drupal\minisite\Controller

Hierarchy

  • class \Drupal\minisite\Controller\AliasController implements \Symfony\Component\DependencyInjection\ContainerAwareInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait

Expanded class hierarchy of AliasController

File

src/Controller/AliasController.php, line 20

Namespace

Drupal\minisite\Controller
View source
class AliasController implements ContainerAwareInterface {
  use ContainerAwareTrait;

  /**
   * Request callback to deliver a single minisite asset.
   *
   * @param int $asset_id
   *   Minisite asset id.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response object.
   */
  public function deliverAsset($asset_id) {
    $asset = Asset::load($asset_id);
    if (!$asset) {
      throw new NotFoundHttpException();
    }
    try {
      $render = $asset
        ->render();
      $response = new Response($render);
    } catch (\Exception $exception) {
      throw new NotFoundHttpException();
    }
    $this
      ->addResponseHeaders($response, $asset);
    return $response;
  }

  /**
   * Add headers to the response object.
   *
   * @param \Symfony\Component\HttpFoundation\Response $response
   *   The response object.
   * @param \Drupal\minisite\Asset $asset
   *   The loaded asset to be used for contextual data.
   */
  protected function addResponseHeaders(Response $response, Asset $asset) {

    // @todo: Review and implement better caching strategy + add tests.
    $response
      ->setPublic();
    $max_age = $asset
      ->getCacheMaxAge();
    $response
      ->setMaxAge($max_age);
    $expires = new \DateTime();
    $expires
      ->setTimestamp(\Drupal::time()
      ->getRequestTime() + $max_age);
    $response
      ->setExpires($expires);
    $response->headers
      ->add($asset
      ->getHeaders());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasController::addResponseHeaders protected function Add headers to the response object.
AliasController::deliverAsset public function Request callback to deliver a single minisite asset.