Route
This class represents a route in the router. You can create an instance of this class directly, but you can also use the methods of the AlteRouter\AlteRouter
class to create a route.
Table of contents
getMethod
Route::getMethod(): string
Get the HTTP method of the route. The HTTP verbs are: GET
, POST
, PUT
, PATCH
, DELETE
, HEAD
and OPTIONS
.
$route = new Route('GET', '/path', 'callback');
var_dump($route->getMethod()); // string(3) "GET"
getPath
Route::getPath(): string
Get the path that the route must match.
$route = new Route('GET', '/path/to/resource', 'callback');
var_dump($route->getPath()); // string(17) "/path/to/resource"
getHandler
Route::getHandler(): callable|string
Get the handler of the route. The handler can be an anonymous function or a string.
$route = new Route('GET', '/path', 'callback');
var_dump($route->getHandler()); // string(8) "callback"
$route = new Route('GET', '/path', function () {
echo 'Hello world';
});
var_dump($route->getHandler()); // php callable
isCallable
Route::isCallable(): bool
Check if the route handler is an anonymous function.
$route = new Route('GET', '/path', 'callback');
var_dump($route->isCallable()); // bool(false)
$route = new Route('GET', '/path', function () {
echo 'Hello world';
});
var_dump($route->isCallable()); // bool(true)
getName
Route::getName(): string|null
Get the name of the route. If the route has no name, the method returns null
.
$route = new Route('GET', '/path', 'callback');
var_dump($route->getName()); // NULL
$route = new Route('GET', '/path', 'callback', 'route_name');
var_dump($route->getName()); // string(10) "route_name"
where
Route::where(string $param, string $regex): Route
Add a constraint on a route parameter. If the constraint is not respected, the route will not match.
string $param
: The name of the parameter.string $regex
: The regular expression that must match with the value of the parameter. It can be a regular expression or a parameter alias (see theAlteRouter\PathParameterAliasRegex
class).
$route = new Route('GET', '/path/{id}', 'callback');
$route->where('id', 'int');
// Or
$route = new Route('GET', '/path/{id}', 'callback');
$route->where('id', '[0-9]+');
match
Route::match(string $path, PathParameterAliasRegex $pathParameterAliasRegex): bool
Check if the path passed in parameter matches with the path of the route. If the path matches, the method returns true
, otherwise it returns false
.
string $path
: The path to match.PathParameterAliasRegex $pathParameterAliasRegex
: The object that contains the parameter aliases.
$route = new Route('GET', '/path/{id:int}', 'callback');
var_dump($route->match('/path/123')); // bool(true)
$route = new Route('GET', '/path/{id}', 'callback');
$route->where('id', 'int');
var_dump($route->match('/path/123')); // bool(true)
$route = new Route('GET', '/path/{id:int}', 'callback');*
var_dump($route->match('/path/abc')); // bool(false)
generate
Route::generate(array $parameters): string
Generate a path from the parameters passed in parameter. If the array contains keys that are not parameters of the route, they will be added at the end of the generated path in query parameters.
array $parameters
: The parameters of the route.
$route = new Route('GET', '/path/{id}', 'callback');
var_dump($route->generate(['id' => 123])); // string(10) "/path/123"
$route = new Route('GET', '/path/{id}', 'callback');
var_dump($route->generate(['id' => 123, 'foo' => 'bar', 'page' => 1])); // string(20) "/path/123?foo=bar&page=1"