成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

PHP異步編程: 基于PHP實(shí)(chao)現(xiàn)(xi) NODEJS web框架KOA

freewolf / 3002人閱讀

摘要:異步編程基于實(shí)現(xiàn)框架說明偶然間在上看到有贊官方倉庫的手把手教你實(shí)現(xiàn)與。由于此前用過,對(duì)于的洋蔥模型嘆為觀止。文檔中是基于擴(kuò)展進(jìn)行開發(fā),而對(duì)并不友好,向來習(xí)慣在下開發(fā)的我一鼓作氣,將改寫并兼容了此項(xiàng)目。

PHP異步編程: 基于 PHP 實(shí)(chao)現(xiàn)(xi) NODEJS web框架 KOA

說明

偶然間在 GITHUB 上看到有贊官方倉庫的 手把手教你實(shí)現(xiàn)co與Koa 。由于此前用過 KOA ,對(duì)于 KOA 的洋蔥模型嘆為觀止。不由得心血來潮的看完了整個(gè)文檔,接著 CTRL+C、CTRL+V 讓代碼跑了起來。
文檔中是基于 swoole 擴(kuò)展進(jìn)行開發(fā),而 swoole 對(duì) WINDOWS 并不友好,向來習(xí)慣在 WINDOWS 下開發(fā)的我一鼓作氣,將Workerman 改寫并兼容了此項(xiàng)目。

體驗(yàn)

PHPKoa Demo 是使用 PHPKoa 開發(fā) HTTP SERVER 的一個(gè)簡(jiǎn)單示例!

安裝
composer require naka1205/phpkoa
使用 Hello World
υse(function(Context $ctx) {
    $ctx->status = 200;
    $ctx->body = "

Hello World

"; }); $app->listen(3000);
Error
υse(new Error());
$app->υse(new NotFound());
$app->υse(new Timeout(3)); //設(shè)置3秒超時(shí)

$router = new Router();

//正常訪問
$router->get("/hello", function(Context $ctx, $next) {
    $ctx->status = 200;
    $ctx->body = "

Hello World

"; }); //訪問超時(shí) $router->get("/timeout", function(Context $ctx, $next) { yield async_sleep(5); }); //訪問出錯(cuò) $router->get("/error", function(Context $ctx, $next) { $ctx->thrοw(500, "Internal Error"); yield; }); $app->υse($router->routes()); $app->listen(3000);
Router
υse(new Error());
$app->υse(new Timeout(5));

$router = new Router();
$router->get("/demo1", function(Context $ctx, $next) {
    $ctx->body = "demo1";
});
$router->get("/demo2", function(Context $ctx, $next) {
    $ctx->body = "demo2";
});
$router->get("/demo3/(d+)", function(Context $ctx, $next, $vars) {
    $ctx->status = 200;
    $ctx->body = "demo3={$vars[0]}";
});
$router->get("/demo4", function(Context $ctx, $next) {
    $ctx->redirect("/demo2");
});

//RESTful API
$router->post("/demo3/(d+)", function(Context $ctx, $next, $vars) {
    //設(shè)置 session
    $ctx->setSession("demo3",$vars[0]);
    //設(shè)置 cookie
    $ctx->setCookie("demo3",$vars[0]);
    $ctx->status = 200;
    $ctx->body = "post:demo3={$vars[0]}";
});
$router->put("/demo3/(d+)", function(Context $ctx, $next, $vars) {

    //獲取單個(gè) cookie
    $cookie_demo3 = $ctx->getCookie("demo3");
    //或者
    $cookies = $ctx->cookies["demo3"];

    //獲取單個(gè) session
    $session_demo3 = $ctx->getSession("demo3");
    //或者
    $session = $ctx->session["demo3"];

    $ctx->status = 200;
    $ctx->body = "put:demo3={$vars[0]}";
});
$router->delete("/demo3/(d+)", function(Context $ctx, $next, $vars) {
    //清除所有 cookie
    $ctx->clearCookie();
    //清除所有 session
    $ctx->clearSession();
    $ctx->status = 200;
    $ctx->body = "delete:demo3={$vars[0]}";
});

//文件上傳
$router->post("/files/(d+)", function(Context $ctx, $next, $vars) {
    $upload_path = __DIR__ . DS .  "uploads" . DS;
    if ( !is_dir($upload_path) ) {
        mkdir ($upload_path , 0777, true);
    }
    $files = [];
    foreach ( $ctx->request->files as $key => $value) {
        if ( !$value["file_name"] || !$value["file_data"] ) {
            continue;
        }
        $file_path = $upload_path . $value["file_name"];
        file_put_contents($file_path, $value["file_data"]);
        $value["file_path"] = $file_path;
        $files[] = $value;
    }

    $ctx->status = 200;
    $ctx->body = json_encode($files);
});


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

$app->listen(3000);
mount("/curl", function() use ($router) {
    $client = new Client();
    $router->get("/get", function( Context $ctx, $next ) use ($client) {
        $r = (yield $client->request("GET", "http://127.0.0.1:3000/demo3/1"));
        $ctx->status = $r->getStatusCode();
        $ctx->body = $r->getBody();
    });

    $router->get("/post", function(Context $ctx, $next ) use ($client){
        $r = (yield $client->request("POST", "http://127.0.0.1:3000/demo3/2"));
        $ctx->status = $r->getStatusCode();
        $ctx->body = $r->getBody();
    });

    $router->get("/put", function( Context $ctx, $next ) use ($client){
        $r = (yield $client->request("PUT", "http://127.0.0.1:3000/demo3/3"));
        $ctx->status = $r->getStatusCode();
        $ctx->body = $r->getBody();
    });

    $router->get("/delete", function( Context $ctx, $next ) use ($client){
        $r = (yield $client->request("DELETE", "http://127.0.0.1:3000/demo3/4"));
        $ctx->status = $r->getStatusCode();
        $ctx->body = $r->getBody();
    });
});

//http://127.0.0.1:5000/files
$router->get("/files", function(Context $ctx, $next ) {
    $client = new Client();
    $r = ( yield $client->request("POST", "http://127.0.0.1:3000/files/2", [
        "multipart" => [
            [
                "name"     => "file_name",
                "contents" => fopen( __DIR__ . "/file.txt", "r")
            ],
            [
                "name"     => "other_file",
                "contents" => "hello",
                "filename" => "filename.txt",
                "headers"  => [
                    "X-Foo" => "this is an extra header to include"
                ]
            ]
        ]
    ]));
    
    $ctx->status = $r->getStatusCode();
    $ctx->body = $r->getBody();
});

// $router->get("/curl/(w+)", function(Context $ctx, $next, $vars) {
//     $method = strtoupper($vars[0]);
//     $client = new Client();
//     $r = (yield $client->request($method, "http://127.0.0.1:3000/demo3/123"));
//     $ctx->status = $r->getStatusCode();
//     $ctx->body = $r->getBody();
// });

$app->υse($router->routes());
$app->listen(5000);
Template

    

{title}

{time}

υse(new Error());
$app->υse(new Timeout(5));

$router = new Router();
$router->get("/hello", function(Context $ctx) {
    $ctx->status = 200;
    $ctx->state["title"] = "HELLO WORLD";
    $ctx->state["time"] = date("Y-m-d H:i:s", time());;
    yield $ctx->render(__DIR__ . "/hello.html");
});
$app->υse($router->routes());

$app->listen(3000);

    

{title}

NameAge
{name} {age}
get("/info", function(Context $ctx) {
    $info = array("name" => "小明", "age" => 15);
    $ctx->status = 200;
    $ctx->state["title"] = "這是一個(gè)學(xué)生信息";
    $ctx->state["info"] = $info;
    yield $ctx->render(__DIR__ . "/info.html");
});

    

{title}

NameAge
{name} {age}
get("/table", function(Context $ctx) {
    $table = array(
        array("name" => "小明", "age" => 15),
        array("name" => "小花", "age" => 13),
        array("name" => "小剛", "age" => 17)
    );
    $ctx->status = 200;
    $ctx->state["title"] = "這是一個(gè)學(xué)生名單";
    $ctx->state["table"] = $table;
    yield $ctx->render(__DIR__ . "/table.html");
});
中間件

靜態(tài)文件處理 中間件 PHPKoa Static




    
    PHPkoa Static
    


    

υse(new Error());
$app->υse(new Timeout(5));
$app->υse(new NotFound()); 
$app->υse(new StaticFiles(__DIR__ . DS .  "static" )); 

$router = new Router();

$router->get("/index", function(Context $ctx, $next) {
    $ctx->status = 200;
    yield $ctx->render(__DIR__ . "/index.html");
});

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

$app->listen(3000);

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/29853.html

相關(guān)文章

  • 前端周報(bào):前端面試題及答案總結(jié);JavaScript參數(shù)傳遞的深入理解

    摘要:前端面試題及答案總結(jié)掘金技術(shù)征文金三銀四,金九銀十,用來形容求職最好的幾個(gè)月。因?yàn)榈拇嬖?,至少在被?biāo)準(zhǔn)化的那一刻起,就支持異步編程了。然而異步編程真正發(fā)展壯大,的流行功不可沒。 showImg(https://segmentfault.com/img/bVVQOH?w=640&h=319); 1、2017前端面試題及答案總結(jié) |掘金技術(shù)征文 金三銀四,金九銀十,用來形容求職最好的幾個(gè)月...

    ermaoL 評(píng)論0 收藏0
  • nodejs異步編程詳解

    摘要:四異步編程解決方案模式模式一定程度上緩解了嵌套回調(diào)的問題,只會(huì)處在未完成完成態(tài)失敗態(tài)中的一種,只會(huì)從未完成轉(zhuǎn)化為完成態(tài)或者失敗態(tài),不能逆轉(zhuǎn)。 一、從一個(gè)簡(jiǎn)單的案例開始 fs.readdir(path.join(__dirname, ./index.js), (err, files) => { files.foreach((filename, index) => { ...

    inapt 評(píng)論0 收藏0
  • 全棧最后一公里 - Node.js 項(xiàng)目的線上服務(wù)器部署與發(fā)布

    摘要:沒有耐心閱讀的同學(xué),可以直接前往學(xué)習(xí)全棧最后一公里。我下面會(huì)羅列一些,我自己錄制過的一些項(xiàng)目,或者其他的我覺得可以按照這個(gè)路線繼續(xù)深入學(xué)習(xí)的項(xiàng)目資源。 showImg(https://segmentfault.com/img/bVMlke?w=833&h=410); 本文技術(shù)軟文,閱讀需謹(jǐn)慎,長(zhǎng)約 7000 字,通讀需 5 分鐘 大家好,我是 Scott,本文通過提供給大家學(xué)習(xí)的方法,...

    Nosee 評(píng)論0 收藏0
  • 【全文】狼叔:如何正確的學(xué)習(xí)Node.js

    摘要:感謝大神的免費(fèi)的計(jì)算機(jī)編程類中文書籍收錄并推薦地址,以后在倉庫里更新地址,聲音版全文狼叔如何正確的學(xué)習(xí)簡(jiǎn)介現(xiàn)在,越來越多的科技公司和開發(fā)者開始使用開發(fā)各種應(yīng)用。 說明 2017-12-14 我發(fā)了一篇文章《沒用過Node.js,就別瞎逼逼》是因?yàn)橛腥嗽谥跎虾贜ode.js。那篇文章的反響還是相當(dāng)不錯(cuò)的,甚至連著名的hax賀老都很認(rèn)同,下班時(shí)讀那篇文章,竟然坐車的還坐過站了。大家可以很...

    Edison 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<