RouterTrait uses trait:short
Provides helper methods to manage the application's router and base URL.
This trait allows you to:
- initialize the internal router parser from an array or a DI container,
- generate URLs for named routes including the base URL,
- redirect responses to named routes.
Note: You can define a baseUrl
in the DI container to be used across all controllers.
Table of Contents
Properties
- $baseUrl : string
- The application's base URL.
- $router : RouteParserInterface
- The router parser instance.
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.
- initializeRouterParser() : static
- Initializes the internal `router` property.
- redirectFor() : ResponseInterface
- Redirects the response to a named route.
- urlFor() : string
- Builds the full URL for a named route including the base URL.
Properties
$baseUrl
The application's base URL.
public
string
$baseUrl
= \oihana\enums\Char::EMPTY
Used as a prefix for all generated URLs.
$router
The router parser instance.
protected
RouteParserInterface
$router
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
initializeRouterParser()
Initializes the internal `router` property.
public
initializeRouterParser([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
The router instance can be provided in the $init
array or fetched
from the DI container if available.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array
- $container : ContainerInterface|null = null
-
Optional DI container
Tags
Return values
static —Returns the current instance for method chaining
redirectFor()
Redirects the response to a named route.
public
redirectFor(ResponseInterface $response, string $name[, array<string|int, mixed> $params = [] ][, int $status = 302 ]) : ResponseInterface
The route URL is generated using $this->router->urlFor()
.
Parameters
- $response : ResponseInterface
-
PSR-7 response instance
- $name : string
-
Name of the route
- $params : array<string|int, mixed> = []
-
Optional associative array of route parameters
- $status : int = 302
-
HTTP redirect status code (default 302)
Return values
ResponseInterface —PSR-7 response with redirect headers
urlFor()
Builds the full URL for a named route including the base URL.
protected
urlFor(string $routeName) : string
Note: Assumes the $router
property is initialized and provides a urlFor()
method.
Parameters
- $routeName : string
-
Name of the route
Return values
string —Full URL including base URL and route path