-
Notifications
You must be signed in to change notification settings - Fork 2
helpers
Webrium provides a set of global helper functions, available everywhere in your application without any use statements. Most are thin wrappers around the framework's core classes, designed for quick, expressive access in routes, controllers, and views.
Generate a full URL for a given relative path.
url('products/list'); // https://example.com/products/list
url(); // https://example.comGet the full URL of the current request.
current_url(); // https://example.com/users/42?tab=settingsGenerate a URL for a named route.
route('users.show', ['id' => 42]); // /users/42Send a response and terminate the request. Arrays and objects are JSON-encoded automatically; any other value is cast to string and sent as-is.
return respond(['status' => 'ok']);
return respond(['error' => 'Not found'], 404);Redirect to a URL and terminate the request. URLs containing line breaks are rejected with an InvalidArgumentException to prevent header injection.
return redirect('/login');
return redirect('/dashboard', 302);Redirect to the previous page, based on the Referer header. Falls back to the application base URL if no referer is present.
return back();These return a ResponsePayload object (with the right Content-Type set) instead of terminating the request, so you return them from a controller and let the framework send them. This is the alternative to respond() when you want to set the content type explicitly or keep building the response.
return html('<h1>Hello</h1>'); // text/html; charset=utf-8
return json(['status' => 'ok']); // application/json; charset=utf-8
return text('plain output'); // text/plain; charset=utf-8
return json(['error' => 'Not found'], 404);html(string $content, int $statusCode = 200): ResponsePayload-
json(mixed $data, int $statusCode = 200): ResponsePayload— encodes the data here; if encoding fails it returns a500error payload instead. text(string $content, int $statusCode = 200): ResponsePayload
Retrieve a value from the current request — query string for GET, body for POST/PUT/PATCH/DELETE (JSON or form-encoded).
input('email'); // a single field
input('page', 1); // with a default
input(); // all input as an arraySee Requests.
Retrieve flashed validation errors from the previous request.
errors(); // all errors: ['field' => 'message', ...]
errors('email'); // error message for 'email', or nullRetrieve the previous request's input value for a field — useful for repopulating forms.
old('email');
old('email', 'default@example.com');Retrieve a flashed message from the previous request.
message(); // full message data
message(true); // raw text onlySee Sessions — Flash Messages, Errors, and Old Input.
All path helpers except root_path() resolve through directory aliases registered in the Directory class, so they reflect your project's actual configured paths. Each accepts an optional sub-path and returns an absolute filesystem path (an empty string if the alias is not registered).
Absolute path inside the public directory.
public_path('images/logo.png');Absolute path inside the app directory.
app_path('Models/User.php');Absolute path inside the application's storage directory.
storage_path('logs/app.log');
storage_path('app/uploads');Absolute path inside the config directory.
config_path('database.php');Absolute path inside the views directory.
resource_path('emails/welcome.php');Absolute path relative to the project root (resolved directly from the application root, not via a directory alias).
root_path('composer.json');Retrieve a value from .env.
env('DB_HOST', '127.0.0.1');
env('APP_DEBUG', false);
env('OPTIONAL_KEY'); // returns null if unsetSee Configuration.
Translate a string using the current locale.
lang('messages.welcome');
lang('messages.greeting', ['name' => 'Alice']);See Localization.
Render the appropriate <script>/<link> tags for Vite assets — the dev server tag in development, or hashed manifest assets in production. Pass an entry point to override the default.
<head>
{!! vite_assets() !!}
{!! vite_assets('resources/js/admin.js') !!}
</head>| Function | Returns | Purpose |
|---|---|---|
url($path) |
string |
Build a full URL |
current_url() |
string |
Current request URL |
route($name, $params) |
string |
URL for a named route |
respond($data, $code) |
never |
Send a response and exit |
redirect($url, $code) |
never |
Redirect and exit |
back() |
never |
Redirect to previous page |
html($content, $code) |
ResponsePayload |
Build an HTML response |
json($data, $code) |
ResponsePayload |
Build a JSON response |
text($content, $code) |
ResponsePayload |
Build a plain-text response |
input($name, $default) |
mixed |
Get request input |
errors($name) |
mixed |
Get flashed validation errors |
old($name, $default) |
mixed |
Get previous input value |
message($raw) |
mixed |
Get flashed message |
public_path($path) |
string |
Path in public/
|
app_path($path) |
string |
Path in app/
|
storage_path($path) |
string |
Path in storage |
config_path($path) |
string |
Path in config/
|
resource_path($path) |
string |
Path in views directory |
root_path($path) |
string |
Path from project root |
env($name, $default) |
mixed |
Get .env value |
lang($key, $replacements) |
string |
Translate a string |
vite_assets($entry) |
string |
Vite asset tags |