1. Go to this page and download the library: Download omegaalfa/wrouter 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/ */
omegaalfa / wrouter example snippets
use Omegaalfa\Wrouter\Router\Wrouter;
use Laminas\Diactoros\Response\JsonResponse;
$router = new Wrouter();
$router->get('/hello', function ($request, $response) {
return new JsonResponse(['message' => 'Hello World']);
});
$response = $router->dispatcher('/hello');
$router->emitResponse($response);
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
class AuthMiddleware implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
// Verificação de autenticação aqui
return $handler->handle($request);
}
}
$routerCache = new RouterCache();
$routerCache->generateRoutes('/api/user/:id', 'GET', function($req, $res) {
// Handler da rota
});