/** * @param Handler $handler */ public static function attachHandler(Handler $handler) { try { self::useMiddleware(new JsonBodyParser()); self::useMiddleware(new QueryStringParser()); $req = new Request(); $res = new Response(); foreach (self::$middleWare as $middleWare) { /** * @var $middleWare IMiddleware */ $middleWare->handle($req, $res); } $content = $handler->handle($req, $res); $headers = $res->getHeaders(); foreach ($headers as $key => $value) { header("{$key}: {$value}"); } http_response_code($res->getStatus()); echo $content; } catch (NotImplementedException $e) { self::handleError(500, $e); } catch (NotFoundException $e) { self::handleError(404, $e); } catch (UnauthorizedException $e) { self::handleError(403, $e); } catch (\Exception $e) { self::handleError(500, $e); } }
<?php require "../vendor/autoload.php"; use Lux\Handler; use Lux\LuxApplication; use Lux\Request; use Lux\Response; $handler = new Handler(); $handler->register("get", "/", function (Request $req, Response $res) { return $res->html("<h1>Hello world!</h1>"); }); $handler->register("get", "/{yourName}", function (Request $req, Response $res) { return $res->json(["hello" => $req->getParam("yourName")]); }); $handler->register("get", "/{firstName}/{lastName}", function (Request $req, Response $res) { return $res->json($req->getParams()); }); $handler->register("put", "/{id}", function (Request $req, Response $res) { return $res->json(["id" => $req->getParam("id"), "req_body" => $req->getParam("body")]); }); $handler->register("delete", "/{id}", function (Request $req, Response $res) { $res->status(204); }); $handler->register("post", "/", function (Request $req, Response $res) { return $res->json($req->getParam("body")); }); $handler->register("get", "/test", function (Request $req, Response $res) { return $res->json("sup"); }); $handler->register("get", "/redirect/{var}", function (Request $req, Response $res) { $res->redirect("/Simple.php/{$req->getParam("var")}");