File Router
File Router
The file router lets you define routes in a PHP file using the Hazaar\Application\Router helper. It is the default router, and route.php is the default source file. Both can be changed in app.php if you need a different setup per environment.
route.php
Define routes in route.php using controller methods, class strings, or closures.
<?php
use Hazaar\Application\Router;
use Hazaar\Controller\Response\View;
use App\Controller\Index;
use App\Controller\API;
Router::get('/', [Index::class, 'index']);
Router::get('/about', function() {
return View('about');
});
Router::get('/api/v1/product', [API::class, 'listProducts']);
Router::post('/api/v1/product', [API::class, 'createProduct']);Loading File Routes
To load a file other than the default route.php, set the file key on the router configuration. Router configuration lives under an environment key so you can define different route sets per environment.
<?php
return [
'development' => [
'router' => [
'type' => 'file',
'file' => 'myCustomRouteFile.php',
],
],
];Loading Routes Programmatically
Routes can also be loaded programmatically using the Router class. This is useful for dynamic route definitions from external sources such as a database.
use Hazaar\Application\Router;
use Hazaar\DBI\Adapter;
$routes = Adapter::create()->table('routes')->find();
foreach ($routes as $route) {
Router::get($route['url'], [$route['controller'], $route['action']]);
}Tips
When loading routes programmatically from a database or other external source, routes are loaded during the bootstrapping process and can impact performance. Consider caching routes to improve performance.
Alternatively for the best performance, it is possible to use PHP in worker mode to pre-load the routes during the bootstrap process. This can provide the best performance as the routes are pre-loaded and do not need to be reloaded on each request. FrankenPHP provides this functionality out of the box and is 100% supported by Hazaar.
See the FrankenPHP documentation for more information.
Route Groups
Route groups provide a way to group routes together, apply prefixes, and share middleware. This is useful for organizing routes and applying common configurations. By grouping routes, you adhere to the DRY (Don't Repeat Yourself) principle, avoiding repetitive prefixes and middleware definitions across multiple routes.
Groups are defined using the Router::group method. The callback function receives a Hazaar\Application\Router\Group instance which can be used to define routes within the group.
use Hazaar\Application\Router;
use Hazaar\Application\Router\Group;
Router::group('admin', function (Group $router) {
// Defines /admin/users
$router->get('users', [AdminController::class, 'users']);
// Defines /admin/settings
$router->get('settings', [AdminController::class, 'settings']);
// Nested group
$router->group('system', function (Group $router) {
// Defines /admin/system/logs
$router->get('logs', [SystemController::class, 'logs']);
});
})->middleware('auth');The above example defines a group with the prefix admin and applies the auth middleware to all routes within the group. It also shows how to nest groups.
For callables, response types, route parameters, default values, and middleware details, see the routing overview.