bnf / slim3-psr15
Slim Framework v3 的 PSR-15 中间件支持
1.1.1
2019-08-20 07:33 UTC
Requires
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- slim/slim: ^3.8
This package is auto-updated.
Last update: 2024-09-20 19:09:37 UTC
README
安装
建议您使用 Composer。
$ composer require bnf/slim3-psr15 "^1.1"
使用方法
创建一个包含以下内容的 index.php 文件
<?php use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Server\MiddlewareInterface as Middleware; use Psr\Http\Server\RequestHandlerInterface as RequestHandler; require 'vendor/autoload.php'; $app = new \Slim\App(); $container = $app->getContainer(); /* Supply a custom callable resolver, which resolves PSR-15 middlewares. */ $container['callableResolver'] = function ($container) { return new \Bnf\Slim3Psr15\CallableResolver($container); }; /* Add a PSR-15 middleware */ $app->add(new class implements Middleware { public function process(Request $request, RequestHandler $handler): Response { $request = $request->withAttribute('msg', 'Hello'); return $handler->handle($request); } }); $app->get('/hello/{name}', new class implements RequestHandler { public function handle(Request $request): Response { $name = $request->getAttribute('name'); $msg = $request->getAttribute('msg'); $response = new \Slim\Http\Response; $response->getBody()->write("$msg, $name"); return $response; } }); $app->run();
您可以使用内置的 PHP 服务器快速测试此内容
$ php -S localhost:8000
访问 https://127.0.0.1:8000/hello/world 将会显示 "Hello, world"。