PathCacheContext.php 877 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. /**
  5. * Defines the PathCacheContext service, for "per URL path" caching.
  6. *
  7. * Cache context ID: 'url.path'.
  8. *
  9. * (This allows for caching relative URLs.)
  10. *
  11. * @see \Symfony\Component\HttpFoundation\Request::getBasePath()
  12. * @see \Symfony\Component\HttpFoundation\Request::getPathInfo()
  13. */
  14. class PathCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static function getLabel() {
  19. return t('Path');
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getContext() {
  25. $request = $this->requestStack->getCurrentRequest();
  26. return $request->getBasePath() . $request->getPathInfo();
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getCacheableMetadata() {
  32. return new CacheableMetadata();
  33. }
  34. }