You are here

RebuildCacheAccessController.php in Rebuild Cache Access 8

File

src/Controller/RebuildCacheAccessController.php
View source
<?php

namespace Drupal\rebuild_cache_access\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Returns responses for rebuild_cache_access module routes.
 */
class RebuildCacheAccessController extends ControllerBase {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * {@inheritdoc}
   */
  public function __construct(RequestStack $request_stack) {
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack'));
  }

  /**
   * Reload the previous page.
   */
  public function reloadPage() {
    $request = $this->requestStack
      ->getCurrentRequest();
    if ($request->server
      ->get('HTTP_REFERER')) {
      return $request->server
        ->get('HTTP_REFERER');
    }
    else {
      return '/';
    }
  }

  /**
   * Rebuild all caches, then redirects to the previous page.
   */
  public function rebuildCache() {
    drupal_flush_all_caches();
    $this
      ->messenger()
      ->addMessage($this
      ->t('All caches cleared.'));
    return new RedirectResponse($this
      ->reloadPage());
  }

}

Classes

Namesort descending Description
RebuildCacheAccessController Returns responses for rebuild_cache_access module routes.