BaseUrlTrait
Provides helper methods for managing the application's base URL.
This trait allows you to:
- dynamically initialize the base URL from an options array or a DI container,
- generate full or partial URLs based on the base URL,
- build URLs for named routes.
Note:
You can define a baseUrl
in your DI container to be used across all controllers.
Table of Contents
Properties
- $baseUrl : string
- The application's base URL.
Methods
- getCurrentPath() : string
- Returns the current application path relative to the base URL.
- getFullPath() : string
- Returns the full application URL including the base URL and optional parameters.
- getPath() : string
- Generates a path based on the base URL and a provided relative path.
- initializeBaseUrl() : static
- Initializes the internal `baseUrl` property.
Properties
$baseUrl
The application's base URL.
public
string
$baseUrl
= \oihana\enums\Char::EMPTY
Used as a prefix for all generated URLs.
Methods
getCurrentPath()
Returns the current application path relative to the base URL.
public
getCurrentPath([ServerRequestInterface|null $request = null ][, array<string|int, mixed> $params = [] ][, bool $useNow = false ]) : string
Uses the Request
object if provided, otherwise falls back to $_SERVER['REQUEST_URI']
.
Allows adding GET parameters via $params
.
Parameters
- $request : ServerRequestInterface|null = null
-
Optional HTTP request
- $params : array<string|int, mixed> = []
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_
parameter with the current timestamp to prevent caching
Return values
string —Full path including the base URL and query parameters
getFullPath()
Returns the full application URL including the base URL and optional parameters.
public
getFullPath([array<string|int, mixed>|null $params = null ][, bool $useNow = false ]) : string
Parameters
- $params : array<string|int, mixed>|null = null
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_
parameter with the current timestamp
Return values
string —Full URL
getPath()
Generates a path based on the base URL and a provided relative path.
public
getPath([string $path = Char::EMPTY ][, array<string|int, mixed>|null $params = null ][, bool $useNow = false ]) : string
Parameters
- $path : string = Char::EMPTY
-
Relative path to append to the base URL
- $params : array<string|int, mixed>|null = null
-
Optional associative array of GET parameters
- $useNow : bool = false
-
If true, adds a
_
parameter with the current timestamp
Return values
string —Full path
initializeBaseUrl()
Initializes the internal `baseUrl` property.
public
initializeBaseUrl([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
The value can come from:
- the
$init
array (keyControllerParam::BASE_URL
), - the DI container if provided and contains the key
ControllerParam::BASE_URL
, - otherwise it remains an empty string.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array
- $container : ContainerInterface|null = null
-
Optional DI container to fetch the base URL
Tags
Return values
static —Returns the current instance for method chaining