documentUrl.php
Table of Contents
Functions
- documentUrl() : string
- Generates a full document URL based on the project's base URL.
Functions
documentUrl()
Generates a full document URL based on the project's base URL.
documentUrl([string $path = Char::EMPTY ][, ContainerInterface|null $container = null ][, string|null $definition = 'baseUrl' ][, bool $trailingSlash = false ]) : string
This helper function is commonly used in IoC container definitions of models to generate the accessible URL of a document or resource.
The function:
- Retrieves the base URL from the DI container using the provided definition key (default 'baseUrl').
- Joins the base URL with the provided relative path.
- Optionally appends a trailing slash.
Example usage:
use Psr\Container\ContainerInterface;
$url = documentUrl('uploads/image.png', $container);
// returns something like 'https://example.com/uploads/image.png'
$urlWithSlash = documentUrl('uploads', $container, 'baseUrl', true);
// returns 'https://example.com/uploads/'
Parameters
- $path : string = Char::EMPTY
-
Relative path of the document (default: empty string).
- $container : ContainerInterface|null = null
-
Optional DI container to fetch the base URL from.
- $definition : string|null = 'baseUrl'
-
Key used to fetch the base URL from the container (default: 'baseUrl').
- $trailingSlash : bool = false
-
Whether to append a trailing slash to the resulting URL (default: false).
Tags
Return values
string —The fully resolved document URL.