chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

電子發(fā)燒友App

硬聲App

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示
創(chuàng)作
電子發(fā)燒友網>電子資料下載>電子資料>MixVega CLI HTTP網絡框架

MixVega CLI HTTP網絡框架

2022-06-30 | zip | 0.03 MB | 次下載 | 免費

資料介紹

授權協(xié)議 Apache
開發(fā)語言 PHP
軟件類型 開源軟件
所屬分類 Web應用開發(fā)、 Web框架

軟件簡介

Vega 是一個用 PHP 編寫的 CLI 模式 HTTP 網絡框架,支持 Swoole、WorkerMan、FPM、CLI-Server

概述

Vega 是?MixPHP?V3+?內置的最核心的組件 (可獨立使用),參考 golang?gin?mux?開發(fā),它包含 Web 應用處理的大量功能 (數據庫處理除外) ,包括:路由、渲染、參數獲取、中間件、文件上傳、靜態(tài)文件處理等;具有 CLI 模式下強大的兼容性,同時支持 Swoole、WorkerMan、FPM、CLI-Server, 并且支持 Swoole 的多種進程模型與協(xié)程。

推薦搭配以下數據庫使用:

推薦文章:

技術交流

知乎:https://www.zhihu.com/people/onanying
官方QQ群:284806582?,?825122875?敲門暗號:vega

安裝

需先安裝?Swoole?或者?WorkerMan

composer require mix/vega

快速開始

  • Swoole 多進程 (異步) 中使用

require __DIR__ . '/vendor/autoload.php';

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');

$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', $vega->handler());
$http->set([
    'worker_num' => 4,
]);
$http->start();

開啟多進程協(xié)程

$http->on('Request', $vega->handler());
$http->on('WorkerStart', function ($server, $workerId) {
    // 協(xié)程初始化
    // 比如:啟動 mix/database mix/redis 的連接池
});
$http->set([
    'enable_coroutine' => true,
    'worker_num' => 4,
]);
php swoole.php
  • Swoole 單進程 (協(xié)程) 中使用

require __DIR__ . '/vendor/autoload.php';

Swoole\Coroutine\run(function () {
    $vega = new Mix\Vega\Engine();
    $vega->handle('/hello', function (Mix\Vega\Context $ctx) {
        $ctx->string(200, 'hello, world!');
    })->methods('GET');
    
    $server = new Swoole\Coroutine\Http\Server('0.0.0.0', 9502, false);
    $server->handle('/', $vega->handler());
    $server->start();
});
php swooleco.php
  • WorkerMan 中使用

require __DIR__ . '/vendor/autoload.php';

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');

$http_worker = new Workerman\Worker("http://0.0.0.0:2345");
$http_worker->onMessage = $vega->handler();
$http_worker->count = 4;
Workerman\Worker::runAll();
php wokerman.php start
  • PHP-FPM 中使用

在?nginx?配置?rewrite?重寫到?index.php


require __DIR__ . '/vendor/autoload.php';

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');
return $vega->run();

這個內置的Web服務器主要用于本地開發(fā)使用,不可用于線上產品環(huán)境。


require __DIR__ . '/vendor/autoload.php';

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');
return $vega->run();
php -S localhost:8000 router.php
  • 訪問測試
% curl http://127.0.0.1:9501/hello
hello, world!

路由配置

配置?Closure?閉包路由

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');

配置?callable?路由

class Hello {
    public function index(Mix\Vega\Context $ctx) {
        $ctx->string(200, 'hello, world!');
    }
}
$vega = new Mix\Vega\Engine();
$vega->handle('/hello', [new Hello(), 'index'])->methods('GET');

配置路由變量

$vega = new Mix\Vega\Engine();
$vega->handle('/users/{id:\d+}', function (Mix\Vega\Context $ctx) {
    $id = $ctx->param('id');
    $ctx->string(200, 'hello, world!');
})->methods('GET');

配置多個?method

$vega = new Mix\Vega\Engine();
$vega->handle('/hello', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET', 'POST');

路由前綴 (分組)

$vega = new Mix\Vega\Engine();
$sub = $vega->pathPrefix('/foo');
$sub->handle('/bar1', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');
$sub->handle('/bar2', function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello1, world!');
})->methods('GET');

參數獲取

請求參數

方法名稱 描述
$ctx->request: ServerRequestInterface 符合PSR的請求對象
$ctx->response: ResponseInterface 符合PSR的響應對象
ctx?>param(stringctx?>param(stringkey): string 獲取路由參數
ctx?>query(stringctx?>query(stringkey): string 獲取url參數,包含路由參數
ctx?>defaultQuery(stringctx?>defaultQuery(stringkey, string $default): string 獲取url參數,可配置默認值
ctx?>getQuery(stringctx?>getQuery(stringkey): string or null 獲取url參數, 可判斷是否存在
ctx?>postForm(stringctx?>postForm(stringkey): string 獲取post參數
ctx?>defaultPostForm(stringctx?>defaultPostForm(stringkey, string $default): string 獲取post參數,可配置默認值
ctx?>getPostForm(stringctx?>getPostForm(stringkey): string or null 獲取post參數,可判斷是否存在

Headers, Cookies, Uri ...

方法名稱 描述
$ctx->contentType(): string 請求類型
ctx?>header(stringctx?>header(stringkey): string 請求頭
ctx?>cookie(stringctx?>cookie(stringname): string cookies
$ctx->uri(): UriInterface 完整uri
$ctx->rawData(): string 原始包數據

客戶端IP

方法名稱 描述
$ctx->clientIP(): string 從反向代理獲取用戶真實IP
$ctx->remoteIP(): string 獲取遠程IP

上傳文件處理

方法名稱 描述
ctx?>formFile(stringctx?>formFile(stringname): UploadedFileInterface 獲取上傳的第一個文件
$ctx->multipartForm(): UploadedFileInterface[] 獲取上傳的全部文件

文件保存

$file = $ctx->formFile('img');
$targetPath = '/data/project/public/uploads/' . $file->getClientFilename();
$file->moveTo($targetPath);

請求上下文

請求當中需要保存一些信息,比如:會話、JWT載荷等。

方法名稱 描述
ctx?>set(stringctx?>set(stringkey, $value): void 設置值
ctx?>get(stringctx?>get(stringkey): mixed or null 獲取值
ctx?>mustGet(stringctx?>mustGet(stringkey): mixed or throws 獲取值或拋出異常

中斷執(zhí)行

abort?執(zhí)行后,會停止執(zhí)行后面的全部代碼,包括中間件。

方法名稱 描述
$ctx->abort(): void 中斷,需自行處理響應
ctx?>abortWithStatus(intctx?>abortWithStatus(intcode): void 中斷并響應狀態(tài)碼
ctx?>abortWithStatusJSON(intctx?>abortWithStatusJSON(intcode, $data): void 中斷并響應JSON
$vega = new Mix\Vega\Engine();
$vega->handle('/users/{id}', function (Mix\Vega\Context $ctx) {
    if (true) {
        $ctx->string(401, 'Unauthorized');
        $ctx->abort();
    }
    $ctx->string(200, 'hello, world!');
})->methods('GET');

響應處理

方法名稱 描述
ctx?>status(intctx?>status(intcode): void 設置狀態(tài)碼
ctx?>setHeader(stringctx?>setHeader(stringkey, string $value): void 設置header
ctx?>setCookie(stringctx?>setCookie(stringname, string?value,intvalue,intexpire = 0, ...): void 設置cookie
ctx?>redirect(stringctx?>redirect(stringlocation, int $code = 302): void 重定向

JSON 請求與輸出

獲取 JSON 請求數據

$vega = new Mix\Vega\Engine();
$vega->handle('/users', function (Mix\Vega\Context $ctx) {
    $obj = $ctx->getJSON();
    if (!$obj) {
        throw new \Exception('Parameter error');
    }
    var_dump($obj);
    $ctx->JSON(200, [
        'code' => 0,
        'message' => 'ok'
    ]);
})->methods('POST');

mustGetJSON?自帶有效性檢查,以下代碼等同于上面

$vega = new Mix\Vega\Engine();
$vega->handle('/users', function (Mix\Vega\Context $ctx) {
    $obj = $ctx->mustGetJSON();
    var_dump($obj);
    $ctx->JSON(200, [
        'code' => 0,
        'message' => 'ok'
    ]);
})->methods('POST');

JSONP 處理

$vega = new Mix\Vega\Engine();
$vega->handle('/jsonp', function (Mix\Vega\Context $ctx) {
    $ctx->JSONP(200, [
        'code' => 0,
        'message' => 'ok'
    ]);
})->methods('GET');

HTML 視圖渲染

創(chuàng)建視圖文件?foo.php

id: $id ?>, name: $name ?>

friends:

    foreach($friends as $name): ?>
  • $name ?>
  • endforeach; ?>

配置視圖路徑,并響應html

$vega = new Mix\Vega\Engine();
$vega->withHTMLRoot('/data/project/views');
$vega->handle('/html', function (Mix\Vega\Context $ctx) {
    $ctx->HTML(200, 'foo', [
        'id' => 1000,
        'name' => '小明',
        'friends' => [
            '小花',
            '小紅'
        ]
    ]);
})->methods('GET');

靜態(tài)文件處理

基于?sendfile?零拷貝,不支持在?PHP-FPM?中使用

$vega = new Mix\Vega\Engine();
$vega->static('/static', '/data/project/public/static');
$vega->staticFile('/favicon.ico', '/data/project/public/favicon.ico');

設置中間件

給某個路由配置中間件,可配置多個

$vega = new Mix\Vega\Engine();
$func = function (Mix\Vega\Context $ctx) {
    // do something
    $ctx->next();
};
$vega->handle('/hello', $func, function (Mix\Vega\Context $ctx) {
    $ctx->string(200, 'hello, world!');
})->methods('GET');

配置全局中間件,即便沒有匹配到路由也會執(zhí)行

$vega = new Mix\Vega\Engine();
$vega->use(function (Mix\Vega\Context $ctx) {
    $ctx->next();
});

前置中間件

$vega->use(function (Mix\Vega\Context $ctx) {
    // do something
    $ctx->next();
});

后置中間件

$vega->use(function (Mix\Vega\Context $ctx) {
    $ctx->next();
    // do something
});

404 自定義

$vega = new Mix\Vega\Engine();
$vega->use(function (Mix\Vega\Context $ctx) {
    try{
        $ctx->next();
    } catch (Mix\Vega\Exception\NotFoundException $ex) {
        $ctx->string(404, 'New 404 response');
        $ctx->abort();
    }
});

500 全局異常捕獲

$vega = new Mix\Vega\Engine();
$vega->use(function (Mix\Vega\Context $ctx) {
    try{
        $ctx->next();
    } catch (\Throwable $ex) {
        if ($ex instanceof Mix\Vega\Abort || $ex instanceof Mix\Vega\Exception\NotFoundException) {
            throw $ex;
        }
        $ctx->string(500, 'New 500 response');
        $ctx->abort();
    }
});

License

Apache License Version 2.0,?http://www.apache.org/licenses/

?

下載該資料的人也在下載 下載該資料的人還在閱讀
更多 >

評論

查看更多

下載排行

本周

  1. 1NS1081/NS1081S/NS1081Q USB 3.0閃存數據手冊
  2. 0.40 MB   |  4次下載  |  2 積分
  3. 2光伏并網逆變器原理
  4. 7.31 MB   |  3次下載  |  2 積分
  5. 3T20電烙鐵原理圖資料
  6. 0.27 MB   |  2次下載  |  免費
  7. 4PL83081 雙路恒流同步降壓轉換器技術手冊
  8. 3.34 MB   |  1次下載  |  免費
  9. 5PL88052 4.8V至60V輸入,5A,同步降壓轉換器技術手冊
  10. 3.36 MB   |  1次下載  |  免費
  11. 6LX8201微孔霧化驅動芯片電路圖資料
  12. 0.15 MB   |  1次下載  |  免費
  13. 7PC6200_7V直流電機驅動器技術手冊
  14. 0.47 MB   |  次下載  |  免費
  15. 8恒溫晶體振蕩器(OCXO)FOC-2D:20.6×20.6mm在通信基站和測試設備中的應用參數規(guī)格
  16. 485.75 KB  |  次下載  |  免費

本月

  1. 1如何看懂電子電路圖
  2. 12.88 MB   |  329次下載  |  免費
  3. 2RK3588數據手冊
  4. 2.24 MB   |  14次下載  |  免費
  5. 3PC5502負載均流控制電路數據手冊
  6. 1.63 MB   |  12次下載  |  免費
  7. 4STM32F10x參考手冊資料
  8. 13.64 MB   |  12次下載  |  1 積分
  9. 5OAH0428 V1.0英文規(guī)格書
  10. 5.86 MB   |  8次下載  |  免費
  11. 6NS1081/NS1081S/NS1081Q USB 3.0閃存數據手冊
  12. 0.40 MB   |  4次下載  |  2 積分
  13. 7PID控制算法學習筆記資料
  14. 3.43 MB   |  3次下載  |  2 積分
  15. 8光伏并網逆變器原理
  16. 7.31 MB   |  3次下載  |  2 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935132次下載  |  10 積分
  3. 2開源硬件-PMP21529.1-4 開關降壓/升壓雙向直流/直流轉換器 PCB layout 設計
  4. 1.48MB  |  420064次下載  |  10 積分
  5. 3Altium DXP2002下載入口
  6. 未知  |  233089次下載  |  10 積分
  7. 4電路仿真軟件multisim 10.0免費下載
  8. 340992  |  191415次下載  |  10 積分
  9. 5十天學會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183349次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81599次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73818次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65990次下載  |  10 積分