1. Go to this page and download the library: Download hotaruma/http-router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hotaruma / http-router example snippets
use Hotaruma\HttpRouter\RouteMap;
$routeMap = new RouteMap();
$routeMap->get('/hello', function () {
echo 'Hello, world!';
});
$routeMap->post('/users', UserController::class);
use Hotaruma\HttpRouter\Attribute\{Route, RouteGroup};
use Hotaruma\HttpRouter\Enum\HttpMethod;
#[RouteGroup(pathPrefix: '/users', methods: [HttpMethod::GET])]
class ApiUserController
{
#[Route('/')]
public function getUsers()
{
// Handle getting users
}
#[Route('/{id}', rules: ['id' => '\d+'])]
public function getUserById(int $id)
{
// Handle getting a user by ID
}
}
use Hotaruma\HttpRouter\RouteScanner\RouteScanner;
$routeScanner = new RouteScanner();
$routeMap = $routeScanner->scanRoutes(ApiController::class);
$routes = $routeMap->getRoutes();
// ...
use Hotaruma\HttpRouter\RouteMap;
$routeMap = new RouteMap();
$routeMap->scanRoutes(UserController::class, PostController::class);
$routeMap->group(
pathPrefix: 'admin',
middlewares: [AdminAccessMiddleware::class],
group: function (RouteMapInterface $routeMap) {
$routeMap->scanRoutes(AdminController::class);
}
);