next / http-server
0.1.1
2024-05-24 10:22 UTC
Requires
- php: ^8.2
- next/di: ~0.1
- next/http-message: ~0.1
- next/routing: ~0.1
- next/utils: ~0.1
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
This package is auto-updated.
Last update: 2024-12-24 11:44:44 UTC
README
符合Psr7规范的兼容多容器的 Http Server
设计思路
request -> kernel -> response
使用
新建类继承\Next\Http\Server\Kernel
类
class HttpKernel extends Kernel { // 全局中间件 protected array $middlewares = []; // 注册路由 protected function map(Router $router) { $router->get('/', 'IndexController@index'); } }
然后使用容器实例化HttpKernel
类
$kernel = \Next\Di\Context::getContainer->make(HttpKernel::class); // 获取一个PsrServerRequest $request = \Next\Http\Message\ServerRequest::createFromGlobals(); // 返回PsrResponse $response = $kernel->handle($request); // 发送响应 (new \Next\Http\Server\ResponseEmitter\FPMResponseEmitter())->emit($response);
框架内置三种环境的ResponseEmitter,均可以自定义
示例
FPM 环境
(function() { $loader = require_once '../vendor/autoload.php'; /** @var Kernel $kernel */ $kernel = Context::getContainer()->make(Kernel::class); $response = $kernel->handle(ServerRequest::createFromGlobals()); (new FPMResponseEmitter())->emit($response); })();
你还可以通过继承Kernel类的方式来改写其中的某些方法或者放入全局中间件