Reference
Here's a reference for the Router
class:
Methods
-
initialize
: Required before defining the first route. Initializes the router by setting default error handling routes (404 and 500). -
route
: Registers a route with the specified HTTP method(s), route pattern, and associated action. -
get
: Registers a route for the HTTP GET method. -
post
: Registers a route for the HTTP POST method. -
put
: Registers a route for the HTTP PUT method. -
delete
: Registers a route for the HTTP DELETE method. -
head
: Registers a route for the HTTP HEAD method. -
options
: Registers a route for the HTTP OPTIONS method. -
patch
: Registers a route for the HTTP PATCH method. -
set404
: Sets a custom handler for the HTTP 404 Not Found error. -
set500
: Sets a custom handler for the HTTP 500 Internal Server Error. -
fallback
: Sets a fallback handler to catch all unmatched routes. -
redirect
: Theredirect
method in Hermes facilitates the definition of redirects from one route to another, allowing for easy navigation control within web applications. -
execute
: Required at the end of the routes declaration. Executes the router, matching the incoming request URI and method to registered routes and invoking associated actions. -
clean
: Resets the router, clearing all registered routes and reinitializing default error handling routes.
initialize()
Initializes the router by setting default error handling routes (404 and 500). This method is required before call the first route.
route(array|string $method, string $route, callable $action): void
Registers a route with the specified HTTP method(s), route pattern, and associated action. The route
method can receive two or more HTTP methods, and can evaluate the same action for these methods.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::route('GET', '/hello', function () {
echo "Hello, World!";
});
Route::route(['GET', 'POST', 'PATCH'], '/world', function () {
echo "See how this method can be used";
});
get(string $route, callable $action): void
Registers a route for the HTTP GET method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::get('/hello', function () {
echo "Hello, World!";
});
post(string $route, callable $action): void
Registers a route for the HTTP POST method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::post('/submit', function () {
echo "Form submitted successfully!";
});
put(string $route, callable $action): void
Registers a route for the HTTP PUT method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::put('/update', function () {
echo "Resource updated successfully!";
});
delete(string $route, callable $action): void
Registers a route for the HTTP DELETE method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::delete('/delete', function () {
echo "Resource deleted successfully!";
});
head(string $route, callable $action): void
Registers a route for the HTTP HEAD method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::head('/info', function () {
echo "Information about the resource.";
});
options(string $route, callable $action): void
Registers a route for the HTTP OPTIONS method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::options('/info', function () {
echo "Options available for the resource.";
});
patch(string $route, callable $action): void
Registers a route for the HTTP PATCH method.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::patch('/update', function () {
echo "Resource partially updated successfully!";
});
set404(callable $action): void
Sets a custom handler for the HTTP 404 Not Found error.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::set404(function () {
echo "404 Not Found: The requested page was not found.";
});
set500(callable $action): void
Sets a custom handler for the HTTP 500 Internal Server Error. This method receive the raised exception as argument.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::set500(function (\Exception $e) {
echo "500 Internal Server Error: There was a server-side issue.";
var_dump($e);
});
fallback(callable $action): void
Sets a fallback handler to catch all unmatched routes.
If this method is set, the set404
method will have no effect. You can refer to the source code for a more complete understanding.
<?php
use AdaiasMagdiel\Hermes\Router;
Router::fallback(function () {
echo "Fallback: Route not found.";
});
redirect(string $from, string $to, bool $permanent = false): void
The redirect
method enables the redirection of traffic from one route to another within the Hermes framework. It takes three arguments:
$from
(string): Specifies the source route from which the redirection occurs.$to
(string): Specifies the destination route to which the traffic is redirected.$permanent
(bool, optional): Determines whether the redirection is permanent (true) or temporary (false). Default is set to false.
<?php
use AdaiasMagdiel\Hermes\Router;
// $permanent set to true (permanent redirect)
Router::redirect("/old-route", "/new-route", true);
// $permanent set to false (temporary redirect)
Router::redirect("/temporary-route", "/new-location", false);
execute(): void
Executes the router, matching the incoming request URI and method to registered routes and invoking associated actions.
This method is necessary at the end of your route definitions to invoke the current route.
clean(): void
Resets the router, clearing all registered routes and reinitializing default error handling routes.