-
Notifications
You must be signed in to change notification settings - Fork 2
helpers
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.
- URL Helpers
- Redirects
- Request Input
- Form Helpers (Old Input & Errors)
- Flash Messages
- Path Helpers
- Environment
- Localization
- Named Routes
- Vite Assets
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=settingsredirect() — 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');
}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();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; ?>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; ?>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/.envenv() — Read an environment variable defined in your .env file:
$debug = env('APP_DEBUG');
// With a default fallback
$dbHost = env('DB_HOST', 'localhost');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!"route() — Generate a URL for a named route:
$url = route('user.show', ['id' => 42]);
// /users/42
$url = route('post.edit', ['id' => 10]);
// /posts/10/editThis is a shortcut for Route::route(). See the Route documentation for how to assign names to routes.
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.