Skip to content

Helpers ​

Table of Contents ​

Introduction ​

Phenix ships global helper functions. These helpers provide shortcuts for common framework operations such as paths, config, URL generation, translation, logging, and value resolution.

base_path ​

Signature:

PHP
base_path(string $path = ''): string

Returns the absolute application base path. Any mixed slash style in the input is normalized.

PHP
$root = base_path();
$configPath = base_path('config/app.php');

response ​

Signature:

PHP
response(): \Phenix\Http\Response

Creates a new response instance.

PHP
return response()->json(['ok' => true]);

For streaming updates to a browser, see Server-sent events.

env ​

Signature:

PHP
env(string $key, Closure|null $default = null): array|string|float|int|bool|null

Reads values from $_ENV.

  • Converts 'true' to true
  • Converts 'false' to false
  • Returns other values as-is
  • Evaluates the default closure only when needed
PHP
$debug = env('APP_DEBUG', static fn (): bool => false);
$name = env('APP_NAME', static fn (): string => 'Phenix');

config ​

Signature:

PHP
config(string $key, mixed $default = null): mixed

Shortcut for Config::get(...).

PHP
$locale = config('app.locale', 'en');

route ​

Signature:

PHP
route(BackedEnum|string $name, array $parameters = [], bool $absolute = true): string

Generates a URL from a named route.

PHP
$url = route('users.show', ['user' => 10]);

url ​

Signature:

PHP
url(string $path, array $parameters = [], bool $secure = false): string

Generates a URL from a path.

PHP
$relative = url('/dashboard');
$secure = url('/dashboard', [], true);

value ​

Signature:

PHP
value(mixed $value, ...$args): mixed

Returns the value directly, or executes it if it is a closure.

PHP
$plain = value(10); // 10
$computed = value(fn (int $x): int => $x * 2, 5); // 10

report ​

Signature:

PHP
report(Throwable $e): void

Logs exception information using Log::error(...), including message, file, line, and trace.

PHP
try {
    // ...
} catch (Throwable $e) {
    report($e);
}

e ​

Signature:

PHP
e(Stringable|string|null $value, bool $doubleEncode = true): string

Escapes a value for HTML output with UTF-8 and safe entity flags.

PHP
echo e('<script>alert(1)</script>');

trans ​

Signature:

PHP
trans(string $key, array $replace = []): array|string

Retrieves a translation line using dot keys.

PHP
echo trans('users.greeting');
echo trans('users.welcome', ['name' => 'John']);

For full translation system details, see Translation.

trans_choice ​

Signature:

PHP
trans_choice(string $key, int $number, array $replace = []): string

Retrieves a pluralized translation line based on the given count.

PHP
echo trans_choice('users.items', 1);
echo trans_choice('users.items', 5);

For advanced pluralization and locale control, see Translation.

class_uses_recursive ​

Signature:

PHP
class_uses_recursive(object|string $class): array

Returns all traits used by a class, including traits used by parent classes and nested traits.

PHP
$traits = class_uses_recursive(App\Models\User::class);

Notes ​

  • Helpers are conditionally declared (function_exists) to avoid redeclaration conflicts.
  • env() checks the resolved value with a truthy check before returning it. Empty strings and '0' may fall back to the default.