Helper Functions
Global Namespace
Section titled “Global Namespace”| Function | Signature | Description |
|---|---|---|
app() | app(): App | Get global app singleton, creating if needed |
// Get the app anywhere$app = app();
// Chain callsapp()->get('/users', fn() => User::all());
// Access container$db = app()->make(DB::class);Verge Namespace
Section titled “Verge Namespace”Import these functions from the Verge namespace:
use function Verge\make;use function Verge\response;use function Verge\json;use function Verge\html;use function Verge\redirect;use function Verge\download;use function Verge\file;use function Verge\route;use function Verge\signed_route;use function Verge\http;use function Verge\config;use function Verge\base_path;Container
Section titled “Container”make()
Section titled “make()”Resolve a class from the container.
make(string $abstract, array $parameters = []): mixed$users = make(UserService::class);$report = make(ReportGenerator::class, ['format' => 'pdf']);Response Helpers
Section titled “Response Helpers”response()
Section titled “response()”Create a basic Response.
response(string $body = '', int $status = 200, array $headers = []): Responseresponse('Hello'); // 200, text/plainresponse('Created', 201); // Custom statusresponse('', 200, ['X-Custom' => 'value']); // Custom headersjson()
Section titled “json()”Create a JSON Response.
json(mixed $data, int $status = 200, array $headers = []): JsonResponsejson(['ok' => true]); // 200, application/jsonjson(['error' => 'fail'], 400); // 400 Bad Requestjson(['users' => $users], 200, [ 'X-Total-Count' => count($users)]);html()
Section titled “html()”Create an HTML Response.
html(string $content, int $status = 200, array $headers = []): HtmlResponsehtml('<h1>Hello World</h1>'); // 200, text/htmlhtml('<h1>Not Found</h1>', 404); // 404 pagehtml($template, 200, [ 'Cache-Control' => 'public, max-age=3600']);redirect()
Section titled “redirect()”Create a redirect Response.
redirect(string $url, int $status = 302, array $headers = []): RedirectResponseredirect('/dashboard'); // 302 temporary redirectredirect('/new-home', 301); // 301 permanent redirectredirect('/login', 302, [ 'X-Redirect-Reason' => 'session-expired']);download()
Section titled “download()”Create a file download Response (Content-Disposition: attachment).
download(string $path, ?string $filename = null, ?string $contentType = null): DownloadResponse// Download with original filenamedownload('/path/to/report.pdf');
// Download with custom filenamedownload('/path/to/file.pdf', 'monthly-report.pdf');
// Download with explicit content typedownload('/path/to/data.bin', 'export.csv', 'text/csv');file()
Section titled “file()”Create a file Response for inline display (Content-Disposition: inline).
file(string $path, ?string $contentType = null): FileResponse// Display PDF in browserfile('/path/to/document.pdf');
// Display imagefile('/path/to/image.png');
// Explicit content typefile('/path/to/data', 'image/svg+xml');Routing Helpers
Section titled “Routing Helpers”route()
Section titled “route()”Generate URL for a named route.
route(string $name, array $params = []): string// Given: $app->get('/users/{id}', ...)->name('users.show');route('users.show', ['id' => 123]); // /users/123
// Given: $app->get('/posts', ...)->name('posts.index');route('posts.index'); // /posts
// With query parameters (extra params become query string)route('users.show', ['id' => 123, 'tab' => 'posts']); // /users/123?tab=postssigned_route()
Section titled “signed_route()”Generate a signed URL for a named route with optional expiration.
signed_route(string $name, array $params = [], ?int $expiration = null): string// Signed URL (no expiration)signed_route('unsubscribe', ['user' => 123]);// /unsubscribe/123?signature=abc123...
// Signed URL with expiration (1 hour from now)signed_route('download', ['file' => 'report.pdf'], time() + 3600);// /download/report.pdf?expires=1699999999&signature=xyz789...
// Use for password reset, email verification, etc.$resetUrl = signed_route('password.reset', [ 'token' => $token], time() + 3600);HTTP Client
Section titled “HTTP Client”http()
Section titled “http()”Get the HTTP client instance for making external requests.
http(): Client// GET request$response = http()->get('https://api.example.com/users');
// POST with JSON body$response = http()->post('https://api.example.com/users', [ 'name' => 'John', 'email' => 'john@example.com']);
// With headers$response = http() ->withHeader('Authorization', 'Bearer ' . $token) ->get('https://api.example.com/me');
// Access response$data = json_decode($response->getBody()->getContents(), true);$status = $response->getStatusCode();Configuration Helpers
Section titled “Configuration Helpers”config()
Section titled “config()”Get or set configuration values.
config(string|array|null $key = null, mixed $default = null): mixed// Get single value$debug = config('app.debug', false);$dbHost = config('database.host', 'localhost');
// Get all config$all = config();
// Set values (pass array)config(['app.debug' => true]);base_path()
Section titled “base_path()”Get the application base path, optionally appending a sub-path.
base_path(string $path = ''): string// Get root path$root = base_path(); // /var/www/myapp
// Get sub-path$storage = base_path('storage'); // /var/www/myapp/storage$views = base_path('resources/views'); // /var/www/myapp/resources/viewsVerge Facade
Section titled “Verge Facade”For static access to the app instance:
use Verge\Verge;
// Create app with optional setupVerge::create(function (App $app) { $app->get('/', fn() => 'Hello');});
// Get current app instance$app = Verge::app();
// Resolve from container$service = Verge::make(UserService::class);$report = Verge::make(ReportGenerator::class, ['format' => 'pdf']);
// Check if boundif (Verge::has(CacheInterface::class)) { // ...}
// Get environment variable$debug = Verge::env('APP_DEBUG', false);
// Generate route URL$url = Verge::route('users.show', ['id' => 123]);
// HTTP client$response = Verge::http()->get('https://api.example.com');
// Cache instance$cache = Verge::cache();$value = $cache->get('key');
// Reset app instance (for testing)Verge::reset();Quick Reference
Section titled “Quick Reference”| Function | Purpose | Example |
|---|---|---|
app() | Get app singleton | app()->make(Service::class) |
make() | Resolve from container | make(UserService::class) |
response() | Basic response | response('OK', 200) |
json() | JSON response | json(['ok' => true]) |
html() | HTML response | html('<h1>Hi</h1>') |
redirect() | Redirect response | redirect('/login') |
download() | File download | download('/path/to/file.pdf') |
file() | Inline file display | file('/path/to/image.png') |
route() | Generate route URL | route('users.show', ['id' => 1]) |
signed_route() | Signed URL | signed_route('reset', [], time() + 3600) |
http() | HTTP client | http()->get('https://api.example.com') |
config() | Get/set config | config('app.debug', false) |
base_path() | Get base path | base_path('storage') |