在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

電子發燒友App

硬聲App

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

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

3天內不再提示
電子發燒友網>電子資料下載>電子資料>MixVega CLI HTTP網絡框架

MixVega CLI HTTP網絡框架

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

資料介紹

授權協議 Apache
開發語言 PHP
操作系統 跨平臺
軟件類型 開源軟件
所屬分類 Web應用開發Web框架

軟件簡介

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

概述

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

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

推薦文章:

技術交流

知乎: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();

開啟多進程協程

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

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服務器主要用于本地開發使用,不可用于線上產品環境。


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 獲取值或拋出異常

中斷執行

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

方法名稱 描述
$ctx->abort(): void 中斷,需自行處理響應
ctx?>abortWithStatus(intctx?>abortWithStatus(intcode): void 中斷并響應狀態碼
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 設置狀態碼
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 視圖渲染

創建視圖文件?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');

靜態文件處理

基于?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');

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

$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. 1涂鴉各WiFi模塊原理圖加PCB封裝
  2. 11.75 MB   |  76次下載  |  1 積分
  3. 2錦銳科技CA51F2 SDK開發包
  4. 24.06 MB   |  29次下載  |  1 積分
  5. 3錦銳CA51F005 SDK開發包
  6. 19.47 MB   |  3次下載  |  1 積分
  7. 4蘋果iphone 11電路原理圖
  8. 4.98 MB   |  3次下載  |  2 積分
  9. 5基礎模擬電子電路
  10. 3.80 MB   |  3次下載  |  1 積分
  11. 6RA-Eco-RA6M4-100PIN-V1.0開發板資料
  12. 34.89 MB  |  1次下載  |  免費
  13. 7STM32F3系列、STM32F4系列、STM32L4系列和STM32L4+系列Cortex-M4編程手冊
  14. 3.32 MB   |  1次下載  |  免費
  15. 8聯想A820t手機維修圖紙包括主板原理圖 尾板原理圖 點位圖
  16. 0.62 MB   |  次下載  |  5 積分

本月

  1. 1AI智能眼鏡產業鏈分析
  2. 4.43 MB   |  383次下載  |  免費
  3. 2蘇泊爾電磁爐線路的電路原理圖資料合集
  4. 2.02 MB   |  296次下載  |  5 積分
  5. 3貼片三極管上的印字與真實名稱的對照表詳細說明
  6. 0.50 MB   |  94次下載  |  1 積分
  7. 4長虹液晶電視R-HS310B-5HF01的電源板電路原理圖
  8. 0.46 MB   |  91次下載  |  5 積分
  9. 5涂鴉各WiFi模塊原理圖加PCB封裝
  10. 11.75 MB   |  76次下載  |  1 積分
  11. 6錦銳科技CA51F2 SDK開發包
  12. 24.06 MB   |  29次下載  |  1 積分
  13. 7AO4803A雙P通道增強型場效應晶體管的數據手冊
  14. 0.11 MB   |  28次下載  |  2 積分
  15. 8長虹液晶彩電LS29機芯的技術資料說明
  16. 3.42 MB   |  16次下載  |  2 積分

總榜

  1. 1matlab軟件下載入口
  2. 未知  |  935127次下載  |  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  |  191388次下載  |  10 積分
  9. 5十天學會AVR單片機與C語言視頻教程 下載
  10. 158M  |  183342次下載  |  10 積分
  11. 6labview8.5下載
  12. 未知  |  81588次下載  |  10 積分
  13. 7Keil工具MDK-Arm免費下載
  14. 0.02 MB  |  73815次下載  |  10 積分
  15. 8LabVIEW 8.6下載
  16. 未知  |  65988次下載  |  10 積分
主站蜘蛛池模板: 夜夜操天天干 | 婷婷色九月| 777奇米影视一区二区三区 | 免费在线公开视频 | 色丁香影院| 天天操夜夜夜 | 免费人成动漫在线播放r18 | 老司机午夜永久在线观看 | 四虎在线精品免费高清在线 | 色综合激情网 | 日日操夜夜操天天操 | 日本高免费观看在线播放 | 日韩成人毛片高清视频免费看 | 日本黄色影片在线观看 | 噜噜嘿 | 黄色大片在线免费观看 | 操操操天天操 | 亚洲邪恶天堂影院在线观看 | 日韩成a人片在线观看日本 日韩成人黄色 | 欧美日韩a | 中文字幕欧美成人免费 | 美女视频黄a全部 | 香蕉视频色版在线观看 | 久久人人干 | 二级黄绝大片中国免费视频 | 在线成人精品国产区免费 | 成人激情综合网 | 久久精品国产免费观看99 | 天天综合网色 | 欧美特黄特色aaa大片免费看 | 成人在线一区二区三区 | 天天干天天干天天色 | 女同国产 | 男人j进人女人j 的视频 | 天天拍拍天天爽免费视频 | 新版天堂中文资源8在线 | 尤物视频黄 | 色多多在线看 | 亚洲黄色性视频 | 好爽毛片一区二区三区四区 | 欧美成人精品一区二三区在线观看 |