# Routing
Phenix routes are based on the amphp/http-server-router (opens new window) package, providing a simple and elegant syntax through a facade:
use Phenix\Facades\Route;
Route::get('/greetings', function () {
return response()->plain('Hello, world!');
});
You can define your routes in the routes/api.php
file. The routes are loaded through the RouteServiceProvider
service provider, in the configuration file config/app.php
, you can see the provider being referenced.
# Available methods
The currently supported HTTP verbs are:
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
# Request object
The server router automatically injects the request into the callback that is invoked by each route:
use Phenix\Facades\Route;
use Amp\Http\Server\Request;
Route::get('/users', function (Request $request) {
// ..
});
# Route parameters
You can pass parameters in routes, such as: IDs of records in the database. The parameters are not injected into the callback but are contained in the Request
object. The amphp/http-server-router (opens new window) package is based on the nikic/FastRoute (opens new window) package:
use Phenix\Http\Attributes;
Route::get('/users/{user}', function (Request $request) {
$attributes = Attributes::fromRequest($request);
$userId = $attributes->integer('user');
// code
});
Multiple route parameters:
Route::get('/invoices/{invoice}/payments/{payment}', function (Request $request) {
$attributes = Attributes::fromRequest($request);
$invoiceId = $attributes->integer('invoice');
$paymentId = $attributes->integer('payment');
// code
});
You can add patterns to route parameters:
Route::get('/users/{user:[0-9]+}', function (Request $request) {
// ..
});
Route::get('/users/{user:\d+}', function (Request $request) {
// ..
});
# Named routes
Route names will allow Phenix to generate URLs easily, you can assign each route a name:
Route::get('/users', function (Request $request) {
// ..
})->name('users.index');
Route::get('/users/{user}', function (Request $request) {
// ..
})->name('users.show');
Note: The URL generation helper will be available soon.
# Route groups
Route groups allow sharing middlewares, assigning route prefixes and route name prefixes. You can create nested route groups and share their properties in the hierarchy in which the routes are defined.
use Phenix\Http\Middlewares\AcceptJsonResponses;
Route::middleware(AcceptJsonResponses::class)
->name('admin')
->prefix('admin')
->group(function (Route $route) {
$route->get('users', fn () => response()->plain('User index'))
->name('users.index');
$route->get('users/{user}', fn () => response()->plain('User details'))
->name('users.show');
$route->name('accounting')
->prefix('accounting')
->group(function (Route $route) {
$route->get('invoices', fn () => response()->plain('Invoice index'))
->name('invoices.index');
$route->prefix('payments')
->name('payments')
->group(function (Route $route) {
$route->get('pending', fn () => response()->plain('Payments index'))
->name('pending.index');
});
});
});
# Route middlewares
Middleware allows you to alter the flow of a request, perform validations, return responses, all using the chain of responsibility design pattern. You can assign one or many middlewares to your routes.
Route::get('/users/{user}', function (Request $request) {
// ..
})->middleware([
SanitizeRequest::class,
CheckUserIsAdmin::class,
]);