Routing
Routes connect an HTTP method and URL to an action. Define them in your route files, name the routes you link to, and use constraints or middleware when an endpoint needs them.
Basic routes
Register routes in app/Routes/web.php or app/Routes/api.php.
Each route file returns a callable that receives the router. The Route facade
delegates registration to the active router.
<?php
use Zap\Core\Http\Route;
use Zap\Core\Http\Router;
use Zap\Core\Http\Response;
return static function (Router $router): void {
Route::get('/', 'HomeController@index')->name('home.index');
Route::get('/health', static fn () => Response::text('OK'));
};
$router directly if preferred, for example
$router->get('/health', ...).
Controller actions
A string action uses the ControllerName@method convention.
Zap resolves the controller from Zap\App\Controllers and injects its
constructor dependencies through the application container.
Route::get('/users', 'UserController@index');
Route::get('/users/{id}', 'UserController@show')
->whereNumber('id')
->name('users.show');
You can also register a callable or a controller class/method pair:
Route::get('/status', 'StatusController@show');
Route::get('/users', [UserController::class, 'index']);
Response, a string, a number, or
null. Controllers commonly return $this->view(...),
$this->json(...), or another response helper.
Route parameters
Put named parameters in braces. The router decodes their URL values and passes them to matching action parameters. Add constraints to limit which paths match.
Route::get('/posts/{id}', 'PostController@show')
->whereNumber('id');
Route::get('/docs/{slug}', 'DocsController@show')
->whereAlphaNumeric('slug');
Route::get('/users/{id}', 'UserController@show')
->where('id', '[0-9]+');
Convenience constraints include:
whereNumber('id')for digits.whereAlpha('name')for ASCII letters.whereAlphaNumeric('slug')for ASCII letters and digits.whereUuid('id')for UUID values.where(['id' => '[0-9]+'])for a custom regular expression.
Named routes
Name a route with name(), then generate its URL with the global
route() helper. Parameters that are not consumed by the path are
appended as a query string.
Route::get('/posts/{id}', 'PostController@show')
->whereNumber('id')
->name('posts.show');
$url = route('posts.show', ['id' => 42]);
// /posts/42
route() helper requires the application router to be bootstrapped
and the requested route name to be registered.
HTTP methods
Register routes for the methods your endpoint needs. any() registers
one action for GET, POST, PUT, PATCH, and DELETE.
Route::get('/posts', 'PostController@index');
Route::post('/posts', 'PostController@store');
Route::put('/posts/{id}', 'PostController@replace');
Route::patch('/posts/{id}', 'PostController@update');
Route::delete('/posts/{id}', 'PostController@destroy');
Route::any('/status', 'StatusController@show');
For browser forms that submit POST, Request supports a
_method value of PUT, PATCH, or
DELETE.
Route groups
Group routes under a shared URL prefix and middleware. Nested groups combine their prefixes and middleware lists.
Route::group([
'prefix' => 'admin',
'middleware' => ['auth'],
], static function (): void {
Route::get('/dashboard', 'DashboardController@index');
Route::get('/users', 'UserController@index');
});
The routes above are registered at /admin/dashboard and /admin/users.
Middleware
Attach middleware to a single route or apply it through a group. Register aliases
in config/middleware.php; a middleware class implements
Zap\Core\Http\Middleware and returns a Response.
Route::get('/account', 'AccountController@show')
->middleware('auth');
Route::post('/profile', 'AccountController@update')
->middleware('auth');
Middleware receives the request, the next handler, and any configured parameters:
use Zap\Core\Http\Response;
public function process(Request $request, callable $next, mixed ...$params): Response
{
if (empty($_SESSION['user'])) {
return Response::text('Unauthorized', 401);
}
return $next($request);
}
config/middleware.php before it is used. A route using an unknown middleware
alias fails during request dispatch.
Learn how controller actions receive route values and return responses.