You are here

public function RedirectChecker::canRedirect in Redirect 8

Determines if redirect may be performed.

Parameters

Request $request: The current request object.

string $route_name: The current route name.

Return value

bool TRUE if redirect may be performed.

File

src/RedirectChecker.php, line 62

Class

RedirectChecker
Redirect checker class.

Namespace

Drupal\redirect

Code

public function canRedirect(Request $request, $route_name = NULL) {
  $can_redirect = TRUE;
  if (isset($route_name)) {
    $route = $this->routeProvider
      ->getRouteByName($route_name);
    if ($this->config
      ->get('access_check')) {

      // Do not redirect if is a protected page.
      $can_redirect = $this->accessManager
        ->checkNamedRoute($route_name, [], $this->account);
    }
  }
  else {
    $route = $request->attributes
      ->get(RouteObjectInterface::ROUTE_OBJECT);
  }
  if (!preg_match('/index\\.php$/', $request
    ->getScriptName())) {

    // Do not redirect if the root script is not /index.php.
    $can_redirect = FALSE;
  }
  elseif (!($request
    ->isMethod('GET') || $request
    ->isMethod('HEAD'))) {

    // Do not redirect if this is other than GET request.
    $can_redirect = FALSE;
  }
  elseif ($this->state
    ->get('system.maintenance_mode') || defined('MAINTENANCE_MODE')) {

    // Do not redirect in offline or maintenance mode.
    $can_redirect = FALSE;
  }
  elseif ($request->query
    ->has('destination')) {
    $can_redirect = FALSE;
  }
  elseif ($this->config
    ->get('ignore_admin_path') && isset($route)) {

    // Do not redirect on admin paths.
    $can_redirect &= !(bool) $route
      ->getOption('_admin_route');
  }
  return $can_redirect;
}