Skip to content

helpers

Benyamin Khalife edited this page May 11, 2026 · 1 revision

Helper Functions

Webrium provides a set of global helper functions that act as convenient shortcuts for the most commonly used operations in your application. They are available everywhere — in controllers, views, and route closures — without any import.


Table of Contents


URL Helpers

url() — Generate an absolute URL for any relative path:

$link = url('products/list');
// https://example.com/products/list

$home = url();
// https://example.com/

current_url() — Get the full URL of the current request:

$current = current_url();
// https://example.com/dashboard?tab=settings

Redirects

redirect() — Redirect the user to any URL. Returns a RequestBack instance so it can be returned directly from a controller:

return redirect('/login');

// With a custom status code
return redirect('/new-page', 301);

back() — Redirect the user back to the previous page using the HTTP_REFERER header:

return back();

Typical use after form validation:

public function store()
{
    $input = input();

    if (empty($input['title'])) {
        return back();
    }

    // ... save and redirect
    return redirect('/posts');
}

Request Input

input() — Retrieve input values from the current request (GET, POST, or JSON body):

// Single field
$name = input('name');

// With a default fallback
$page = input('page', 1);

// All input as an associative array
$all = input();

Form Helpers (Old Input & Errors)

These helpers make it easy to repopulate form fields and display validation errors after a failed submission.

old() — Retrieve the previously submitted value for a field:

<input type="text" name="email" value="<?= old('email') ?>">

// With a default fallback
<input type="text" name="country" value="<?= old('country', 'US') ?>">

errors() — Retrieve validation error messages carried over from the previous request:

// Get the error for a specific field
$error = errors('email');

// Get all errors as an array
$allErrors = errors();

Example in a view:

<input type="text" name="email" value="<?= old('email') ?>">
<?php if ($error = errors('email')): ?>
    <span class="error"><?= $error ?></span>
<?php endif; ?>

Flash Messages

message() — Retrieve a flash message set during the previous request:

// Get the full message object (includes metadata)
$msg = message();

// Get only the message text
$text = message(true);

Example in a view:

<?php if ($text = message(true)): ?>
    <div class="alert"><?= $text ?></div>
<?php endif; ?>

Path Helpers

These helpers return absolute filesystem paths, useful when reading or writing files in your application.

// Path inside the public directory
$path = public_path('images/logo.png');
// /var/www/app/public/images/logo.png

// Path inside the app directory
$path = app_path('Models/User.php');
// /var/www/app/app/Models/User.php

// Path inside the storage directory
$path = storage_path('logs/app.log');
// /var/www/app/storage/logs/app.log

// Path relative to the application root
$path = root_path('.env');
// /var/www/app/.env

Environment

env() — Read an environment variable defined in your .env file:

$debug = env('APP_DEBUG');

// With a default fallback
$dbHost = env('DB_HOST', 'localhost');

Localization

lang() — Translate a key into the current locale's string:

$text = lang('auth.login_failed');
// "Invalid email or password."

// With dynamic replacements
$text = lang('welcome.greeting', ['name' => 'John']);
// "Welcome, John!"

Named Routes

route() — Generate a URL for a named route:

$url = route('user.show', ['id' => 42]);
// /users/42

$url = route('post.edit', ['id' => 10]);
// /posts/10/edit

This is a shortcut for Route::route(). See the Route documentation for how to assign names to routes.


Vite Assets

vite_assets() — Output the correct HTML <script> and <link> tags for your Vite entry point. Handles both development and production modes automatically:

<!DOCTYPE html>
<html>
<head>
    <?= vite_assets() ?>
</head>
<body>
    ...
</body>
</html>
  • In development, outputs the Vite dev server <script> tag.
  • In production, reads the hashed asset manifest and outputs the correct <script> and <link> tags.

Clone this wiki locally