- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚使用自己的设置通过 composer 安装了一个全新的 slim 副本。非常简单的 index.php,里面很少:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require_once __DIR__ . '/../bootstrap.php';
// start the app
$APP = AppFactory::create();
/**
* Middleware to check validation before any routes
*/
$APP->add(function(Request $request, Response $response, callable $next){
$response = $next($request,$response);
return $response;
});
/**
* Add routes
*/
$APP->get('/test',function(Request $request, Response $response, array $args){
return $response->getBody()->write('hello');
});
// run the app
$APP->run();
PHP 给出了一个非常奇怪的错误:
**Fatal error: Uncaught TypeError: Argument 2 passed to {closure}() must be an instance of Psr\Http\Message\ResponseInterface, instance of Slim\Routing\RouteRunner given, called in /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php on line 275 and defined in /var/www/public/index.php:16 Stack trace: #0 /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php(275): {closure}(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
1 /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): class@anonymous->handle(Object(Slim\Psr7\Request)) #2
/var/www/vendor/slim/slim/Slim/App.php(206): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 /var/www/vendor/slim/slim/Slim/App.php(190): Slim\App->handle(Object(Slim\Psr7\Request)) #4 /var/www/public/index.php(34): Slim\App->run() #5 {main} thrown in /var/www/public/index.php on line 16**
我不明白为什么它说这里的基本中间件正在使用 Slim\Routing\RouteRunner 的实例,而我清楚地给它 Psr\Http\Message\ResponseInterface
有什么想法吗?
编辑:
感谢 delboy 的回答,但你能更具体一点吗? slim 的文档显示像这样使用它(http://www.slimframework.com/docs/v3/concepts/middleware.html):
$app->add(function ($request, $response, $next) {
$response->getBody()->write('BEFORE');
$response = $next($request, $response);
$response->getBody()->write('AFTER');
return $response;
});
但这行不通!总是出现类型错误,他们的文档是否已过时?如果是这样,我如何在这里实现中间件?
他们给出的这个示例代码还产生了另一个奇怪的错误:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function {closure}(), 2 passed in /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php on line 275 and exactly 3 expected in /var/www/public/index.php:26 Stack trace: #0 /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php(275): {closure}(Object(Slim\Psr7\Request), Object(class@anonymous)) #1 /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php(73): class@anonymous->handle(Object(Slim\Psr7\Request)) #2 /var/www/vendor/slim/slim/Slim/App.php(206): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 /var/www/vendor/slim/slim/Slim/App.php(190): Slim\App->handle(Object(Slim\Psr7\Request)) #4 /var/www/public/index.php(38): Slim\App->run() #5 {main} thrown in /var/www/public/index.php on line 26
我的示例代码传递了 3 个参数,而不是 2 个!
编辑:
好吧,Slim 文档显然已经过时了,应该这样做:
$app->add(function(ServerRequestInterface $request, RequestHandlerInterface $handler) {
$response = new Response();
$response->getBody()->write('STUFF');
return $response;
});
最佳答案
您的中间件未实现 PSR-15。您不应传递响应,而应传递请求处理程序接口(interface):
namespace Psr\Http\Server;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Participant in processing a server request and response.
*
* An HTTP middleware component participates in processing an HTTP message:
* by acting on the request, generating the response, or forwarding the
* request to a subsequent middleware and possibly acting on its response.
*/
interface MiddlewareInterface
{
/**
* Process an incoming server request.
*
* Processes an incoming server request in order to produce a response.
* If unable to produce the response itself, it may delegate to the provided
* request handler to do so.
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface;
}
关于php - Slim 框架给出了一个令人困惑的未捕获类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57770024/
很抱歉新手的问题,但是: 我最近才发现“=”运算符不只是处理对象/等等。值(value),也是引用。这很酷,但我认为这对变量来说是不一样的,它不会在存储整数或 float 的变量之间创建引用。后来我觉
我是一名优秀的程序员,十分优秀!