CacheableRedirectResponse example

    // response passed to TrustedRedirectResponse::createFromRedirectResponse()     // is itself cacheable.     $expected_cacheability = ($redirect_response instanceof CacheableResponseInterface) ? $redirect_response->getCacheableMetadata() : (new CacheableMetadata())->setCacheMaxAge(0);
    $this->assertEquals($expected_cacheability$trusted_redirect_response->getCacheableMetadata());
  }

  /** * @return array */
  public function providerCreateFromRedirectResponse() {
    return [
      'cacheable-with-tags' => [(new CacheableRedirectResponse('/example'))->addCacheableDependency((new CacheableMetadata())->addCacheTags(['foo']))],
      'cacheable-with-max-age-0' => [(new CacheableRedirectResponse('/example'))->addCacheableDependency((new CacheableMetadata())->setCacheMaxAge(0))],
      'uncacheable' => [new RedirectResponse('/example')],
    ];
  }

}
// It is impossible to create a link or a route to a path starting with     // multiple leading slashes. However if a form is added to the 404 page that     // submits back to the same URI this presents an open redirect     // vulnerability. Also, Drupal 7 renders the same page for     // http://www.example.org/foo and http://www.example.org////foo.     if (str_contains($path, '//')) {
      $path = preg_replace('/\/+/', '/', $path);
      $qs = $request->getQueryString();
      if ($qs) {
        $qs = '?' . $qs;
      }
      $event->setResponse(new CacheableRedirectResponse($request->getUriForPath($path) . $qs));
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    $events[KernelEvents::REQUEST][] = ['redirect', 1000];
    return $events;
  }

}
Home | Imprint | This part of the site doesn't use cookies.