-
Notifications
You must be signed in to change notification settings - Fork 2
route
The Route class is the core routing engine of Webrium. It allows you to register URL patterns for different HTTP methods, group routes with shared prefixes or middleware, generate URLs by name, and handle 404 responses.
- Basic Routing
- HTTP Methods
- Route Parameters
- Named Routes
- Route Groups
- Controllers
- Middleware
- 404 Not Found Handler
- Loading Route Files
- Running the Router
- API Reference
The simplest route accepts a URL pattern and a closure:
use Webrium\Route;
Route::get('/', function () {
return 'Hello, World!';
});
Route::get('/about', function () {
return 'About page';
});The value returned from the closure is automatically sent as the response.
Webrium supports the most common HTTP methods:
Route::get('/users', function () { /* ... */ });
Route::post('/users', function () { /* ... */ });
Route::put('/users/{id}', function ($id) { /* ... */ });
Route::delete('/users/{id}', function ($id) { /* ... */ });
// Matches any HTTP method
Route::any('/ping', function () {
return 'pong';
});Dynamic segments are defined with curly braces {name} and are passed as arguments to the handler in the order they appear in the URL:
Route::get('/users/{id}', function ($id) {
return "User ID: $id";
});
Route::get('/posts/{year}/{slug}', function ($year, $slug) {
return "Post: $slug ($year)";
});Note: Parameter names must start with a letter or underscore and contain only letters, digits, or underscores.
Assigning a name to a route makes it easy to generate URLs without hardcoding paths.
Option 1 — Pass the name directly:
Route::get('/users/{id}', function ($id) {
return "User $id";
}, 'user.show');Option 2 — Chain the name() method:
Route::get('/users/{id}', function ($id) {
return "User $id";
})->name('user.show');Generating a URL from a named route:
$url = Route::route('user.show', ['id' => 42]);
// Returns: /users/42If a required parameter is missing or the name is not registered, a debug error is triggered.
Groups let you share a prefix, middleware, or both across multiple routes without repeating yourself.
Route::group('/api', function () {
Route::get('/users', function () {
return 'GET /api/users';
});
Route::post('/users', function () {
return 'POST /api/users';
});
});Groups can be nested:
Route::group('/api', function () {
Route::group('/v1', function () {
Route::get('/status', function () {
return 'GET /api/v1/status';
});
});
});Route::group(['middleware' => 'AuthMiddleware@handle'], function () {
Route::get('/dashboard', function () {
return 'Protected dashboard';
});
});Multiple middleware can be passed as an array:
Route::group(['middleware' => ['AuthMiddleware@check', 'RoleMiddleware@admin']], function () {
Route::get('/admin', function () {
return 'Admin panel';
});
});Route::group(['prefix' => '/admin', 'middleware' => 'AuthMiddleware@check'], function () {
Route::get('/users', function () {
return 'GET /admin/users';
});
Route::delete('/users/{id}', function ($id) {
return "Deleted user $id";
});
});Instead of a closure, a route handler can be a "Controller@method" string. Webrium will automatically load and call the appropriate controller method.
Route::get('/users', 'UserController@index');
Route::get('/users/{id}', 'UserController@show');
Route::post('/users', 'UserController@store');
Route::put('/users/{id}', 'UserController@update');
Route::delete('/users/{id}', 'UserController@destroy');Route parameters are passed as arguments to the controller method in order:
// UserController.php
class UserController
{
public function show($id)
{
return "User: $id";
}
}Controller files are loaded from the controllers directory defined in your app configuration.
Middleware is defined at the group level and controls whether a matched route should be executed. A middleware must return a truthy value to allow the request through; returning falsy causes the router to skip to the next matching route.
Callable middleware:
Route::group(['middleware' => function () {
return isset($_SESSION['user']); // allow if logged in
}], function () {
Route::get('/profile', function () {
return 'My profile';
});
});String middleware (Controller@method format):
Route::group(['middleware' => 'AuthMiddleware@handle'], function () {
Route::get('/settings', function () {
return 'Settings';
});
});Multiple middleware (array):
Each middleware in the array is executed in order. If any one returns falsy, access is denied.
Route::group(['middleware' => [
'AuthMiddleware@handle',
'SubscriptionMiddleware@check',
]], function () {
Route::get('/premium', function () {
return 'Premium content';
});
});By default, Webrium outputs Page not found with a 404 status code when no route matches. You can replace this with your own handler:
Closure:
Route::setNotFoundHandler(function () {
return 'Custom 404 — Page not found';
});Controller:
Route::setNotFoundHandler('ErrorController@notFound');For larger applications, you can split your routes into separate files stored in the routes directory:
// index.php or bootstrap file
Route::source(['web.php', 'api.php']);Each file is included once. If a file is not found, a debug error is triggered.
Example routes/api.php:
use Webrium\Route;
Route::group(['prefix' => '/api/v1', 'middleware' => 'AuthMiddleware@handle'], function () {
Route::get('/users', 'UserController@index');
Route::post('/users', 'UserController@store');
Route::get('/users/{id}', 'UserController@show');
});The router is started by calling App::run(), which internally calls Route::run(). You do not need to call Route::run() directly in normal usage.
use Webrium\App;
use Webrium\Route;
App::initialize(__DIR__);
Route::get('/', function () {
return 'Hello';
});
App::run(); // starts the router| Method | Description |
|---|---|
Route::get($url, $handler, $name = '') |
Register a GET route |
Route::post($url, $handler, $name = '') |
Register a POST route |
Route::put($url, $handler, $name = '') |
Register a PUT route |
Route::delete($url, $handler, $name = '') |
Register a DELETE route |
Route::any($url, $handler, $name = '') |
Register a route for any HTTP method |
All registration methods return a Route instance so you can chain .name().
| Method | Description |
|---|---|
->name(string $name) |
Assign a name to the last registered route |
Route::group($options, callable $callback) |
Group routes with a shared prefix and/or middleware |
Route::route(string $name, array $params = []) |
Generate a URL for a named route |
Route::setNotFoundHandler($handler) |
Set a custom 404 handler |
Route::source(array $fileNames) |
Load route files from the routes directory |
| Parameter | Type | Description |
|---|---|---|
$url |
string |
URL pattern, e.g. /users/{id}
|
$handler |
callable|string |
Closure or "Controller@method" string |
$name |
string |
Optional route name for URL generation |
$options |
string|array |
Prefix string, or array with prefix and/or middleware keys |