Adds a JavaScript or CSS resource to the page.
@param string $source File path or inline code of the resource
@param int $type Resource type: JS or CSS (defaults to JS)
@param int $position Position where the resource should be included (head, footer, inline, etc.)
@param bool $compress Whether to compress/minify the resource (default true)
@return $this Fluent interface
addResource(string $source, int $type = self::RESOURCE_TYPE_JS, int $position = self::RESOURCE_POS_HEAD, bool $compress = true): self$type:
| Constant | Definition |
|---|---|
| RESOURCE_TYPE_JS | Javascript file |
| RESOURCE_TYPE_CSS | CSS file |
$position:
| Constant | Definition |
|---|---|
| RESOURCE_POS_HEAD | Adds file reference to the header |
| RESOURCE_POS_FOOTER | Adds file reference to the footer |
| RESOURCE_POS_INLINE | Adds inline code to the header |
| RESOURCE_POS_INLINE_FOOTER | Adds inline code to the footer |
$this->addResource('path/to/the/file', parent::RESOURCE_TYPE_CSS, parent::RESOURCE_POS_FOOTER, true);Adds a single parameter to the response parameters array.
@param string $key Parameter key
@param mixed $value Parameter value
@return $this Fluent interface
addResponseParameter(string $key, mixed $value): selfAdds multiple parameters to the response parameters array.
@param array $parameters Associative array of parameters to add
@return $this Fluent interface
addResponseParameters(array $parameters): selfSets JSON encoding flags for JSON responses.
@param int $flags Bitmask of JSON encoding flags
@return $this Fluent interface
setJSONFlags(int $flags): selfSets the allowed HTTP methods for the request.
@param array|null $allowedMethods Array of allowed methods or null to unset
@return $this Fluent interface
setAllowedMethods(?array $allowedMethods): selfSets the block size used for streaming files.
@param int|null $blockSize Block size in bytes or null to unset
@return $this Fluent interface
setBlockSize(?int $blockSize): selfSets the file redirect method used (none, Apache, Nginx).
@param int $fileRedirect File redirect type constant
@return $this Fluent interface
setFileRedirect(int $fileRedirect): selfSets the file redirect mask, used for Nginx's X-Accel-Redirect.
@param string|null $fileRedirectMask Mask path or null to unset
@return $this Fluent interface
setFileRedirectMask(?string $fileRedirectMask): selfSets the file redirect relative directory, used for Nginx's X-Accel-Redirect.
@param string|null $fileRedirectRelDir Relative directory path or null to unset
@return $this Fluent interface
setFileRedirectRelDir(?string $fileRedirectRelDir): selfSets the CSRF token for request protection.
@param string $csrfToken CSRF token string
@return $this Fluent interface
setCsrfToken(string $csrfToken): selfAdds an entry to the import map.
@param string $key The key/specifier for the import map entry
@param string $path The file path to the asset
@param bool $isPublicResource If true, converts path to public web URL using PathUtility; if false, uses raw path
@param int $moduleSpecifierKey The import map section to use (default: self::IMPORTMAP_IMPORTS)
@return self Fluent interface for chaining
addImportmapEntry(string $key, string $path, bool $isPublicResource = true, int $moduleSpecifierKey = self::IMPORTMAP_IMPORTS): selfImport map module specifier keys:
| Constant | Description |
|---|---|
| IMPORTMAP_IMPORTS | Uses the “imports” key in the map. |
| IMPORTMAP_INTEGRITY | Uses the “integrity” key in the map. |
| IMPORTMAP_SCOPES | Uses the “scopes” key in the map. |
$this->addImportmapEntry('jquery', 'EXT:extension/Resources/Public/JS/jquery.js');Result:
<script type="importmap">
{
"imports": {
"jquery": "/_assets/.../JS/jquery.js"
}
}
</script>