class AssetRoutes

Same name and namespace in other branches
  1. 11.x core/modules/system/src/Routing/AssetRoutes.php \Drupal\system\Routing\AssetRoutes

Defines a routes' callback to register a URL for serving assets.

Hierarchy

Expanded class hierarchy of AssetRoutes

File

core/modules/system/src/Routing/AssetRoutes.php, line 13

Namespace

Drupal\system\Routing
View source
class AssetRoutes implements ContainerInjectionInterface {
  
  /**
   * Constructs an asset routes object.
   *
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $streamWrapperManager
   *   The stream wrapper manager service.
   */
  public function __construct(protected readonly StreamWrapperManagerInterface $streamWrapperManager) {
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('stream_wrapper_manager'));
  }
  
  /**
   * Returns an array of route objects.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   An array of route objects.
   */
  public function routes() : array {
    $routes = [];
    // Generate assets. If clean URLs are disabled image derivatives will always
    // be served through the routing system. If clean URLs are enabled and the
    // image derivative already exists, PHP will be bypassed.
    $directory_path = $this->streamWrapperManager
      ->getViaScheme('assets')
      ->getDirectoryPath();
    $routes['system.css_asset'] = new Route('/' . $directory_path . '/css/{file_name}', [
      '_controller' => 'Drupal\\system\\Controller\\CssAssetController::deliver',
    ], [
      '_access' => 'TRUE',
    ]);
    $routes['system.js_asset'] = new Route('/' . $directory_path . '/js/{file_name}', [
      '_controller' => 'Drupal\\system\\Controller\\JsAssetController::deliver',
    ], [
      '_access' => 'TRUE',
    ]);
    return $routes;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AssetRoutes::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
AssetRoutes::routes public function Returns an array of route objects.
AssetRoutes::__construct public function Constructs an asset routes object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.