υse(new Bo…" />
返回介绍

一个综合示例

发布于 2024-08-15 12:54:36 字数 2698 浏览 0 评论 0 收藏 0

一个综合示例

<?php

$app = new Application();

$app->υse(new Logger());
$app->υse(new Favicon(__DIR__ . "/favicon.iRco"));
$app->υse(new BodyDetecter()); // 输出特定格式body
$app->υse(new ExceptionHandler()); // 处理异常
$app->υse(new NotFound()); // 处理404
$app->υse(new RequestTimeout(200)); // 处理请求超时, 会抛出HttpException


$router = new Router();

$router->get('/index', function(Context $ctx) {
    $ctx->status = 200;
    $ctx->state["title"] = "HELLO WORLD";
    $ctx->state["time"] = date("Y-m-d H:i:s", time());;
    $ctx->state["table"] = $_SERVER;
    yield $ctx->render(__DIR__ . "/index.html");
});

$router->get('/404', function(Context $ctx) {
    $ctx->status = 404;
});

$router->get('/bt', function(Context $ctx) {
    $ctx->body = "<pre>" . print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), true);
});

$router->get('/timeout', function(Context $ctx) {
    // 超时
    yield async_sleep(500);
});

$router->get('/error/http_exception', function(Context $ctx) {
    // 抛出带status的错误
    $ctx->thrοw(500, "Internal Error");
    // 等价于 throw new HttpException(500, "Internal Error");
    yield;
});

$router->get('/error/exception', function(Context $ctx) {
    // 直接抛出错误, 500 错误
    throw new \Exception("some internal error", 10000);
    yield;
});

$router->get('/user/{id:\d+}', function(Context $ctx, $next, $vars) {
    $ctx->body = "user={$vars['id']}";
    yield;
});

// http://127.0.0.1:3000/request/www.baidu.com
$router->get('/request/{url}', function(Context $ctx, $next, $vars) {
    $r = (yield async_curl_get($vars['url']));
    $ctx->body = $r->body;
    $ctx->status = $r->statusCode;
});

$app->υse($router->routes());


$app->υse(function(Context $ctx) {
    $ctx->status = 200;
    $ctx->body = "<h1>Hello World</h1>";
    yield;
});

$app->listen(3000);

以上我们完成了一个基于swoole的版本的php-koa。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文