Oihana PHP System

AppTrait uses trait:short

Trait providing helpers to manage the application instance and generate URLs.

This trait allows controllers to access the Slim App instance, retrieve the application's base path, and build URLs with optional query parameters.

Note: You can define a baseUrl in the DI container to use it consistently across all controllers.

Table of Contents

Properties

$baseUrl  : string
The application's base URL.
$app  : App
The Slim App instance.

Methods

getBasePath()  : string
Returns the base path of the application.
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.
getUrl()  : string
Generates a full URL for the application.
initializeApp()  : static
Initializes the internal `app` property.
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.

$app

The Slim App instance.

protected App $app
Tags
access

protected

Methods

getBasePath()

Returns the base path of the application.

public getBasePath() : string

This corresponds to the path configured in Slim and can be used to generate URLs relative to the application root.

Return values
string

The application's base path (e.g., "/myapp")

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

getUrl()

Generates a full URL for the application.

public getUrl([string $path = '' ][, array<string|int, mixed> $params = [] ][, bool $useNow = false ]) : string

The URL is constructed using the application's base URL, the base path, the optional path, and query parameters.

Parameters
$path : string = ''

Optional relative path to append to the base URL.

$params : array<string|int, mixed> = []

Optional query parameters as key-value pairs.

$useNow : bool = false

If true, timestamp-like parameters will be processed immediately.

Return values
string

The full URL including base URL, path, and query parameters.

initializeApp()

Initializes the internal `app` property.

public initializeApp([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static

This method retrieves the Slim App instance from either the provided initialization array or the DI container. It throws an exception if no valid App instance can be found.

Parameters
$init : array<string|int, mixed> = []

Optional initialization array (e.g., ['app' => App instance]).

$container : ContainerInterface|null = null

Optional DI container for retrieving the App instance.

Tags
throws
NotFoundExceptionInterface

If the container is used and the App class is not found.

throws
ContainerExceptionInterface

If the container throws an internal error.

throws
RuntimeException

If no valid App instance is provided or found.

Return values
static

Returns the current controller instance for method chaining.

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 (key ControllerParam::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
throws
ContainerExceptionInterface

If the container encounters an error during access

throws
NotFoundExceptionInterface

If the ControllerParam::BASE_URL key is not found in the container

Return values
static

Returns the current instance for method chaining


        
On this page

Search results