摘要:異步編程基于實(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)目。
PHPKoa Demo 是使用 PHPKoa 開發(fā) HTTP SERVER 的一個(gè)簡(jiǎn)單示例!
安裝composer require naka1205/phpkoa使用 Hello World
υse(function(Context $ctx) { $ctx->status = 200; $ctx->body = "ErrorHello World
"; }); $app->listen(3000);
υ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 = "RouterHello 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);
υ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}
Name | Age |
{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}
Name | Age |
{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
摘要:前端面試題及答案總結(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è)月...
摘要:四異步編程解決方案模式模式一定程度上緩解了嵌套回調(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) => { ...
摘要:沒有耐心閱讀的同學(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í)的方法,...
摘要:感謝大神的免費(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í)讀那篇文章,竟然坐車的還坐過站了。大家可以很...
閱讀 2404·2023-04-25 20:07
閱讀 3330·2021-11-25 09:43
閱讀 3703·2021-11-16 11:44
閱讀 2554·2021-11-08 13:14
閱讀 3204·2021-10-19 11:46
閱讀 922·2021-09-28 09:36
閱讀 3038·2021-09-22 10:56
閱讀 2410·2021-09-10 10:51