为了账号安全,请及时绑定邮箱和手机立即绑定

Slim 框架提供了一个令人困惑的未捕获类型错误

Slim 框架提供了一个令人困惑的未捕获类型错误

PHP
慕工程0101907 2021-12-24 15:05:26
我刚刚使用我自己的设置通过 composer 安装了一个新的 slim 副本。非常简单的 index.php,里面的内容很少:<?phpuse 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给出了一个非常奇怪的错误:**致命错误:未捕获的类型错误:传递给 {closure}() 的参数 2 必须是 Psr\Http\Message\ResponseInterface 的实例,给出的 Slim\Routing\RouteRunner 实例,在 /var/www/vendor/slim/ 中调用slim/Slim/MiddlewareDispatcher.php 位于第 275 行,定义在 /var/www/public/index.php:16 堆栈跟踪:#0 /var/www/vendor/slim/slim/Slim/MiddlewareDispatcher.php(275): {closure}(对象(Slim\Psr7\Request),对象(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} 在第 16 行的 /var/www/public/index.php 中抛出**我不明白为什么它说这里的基本中间件正在使用 Slim\Routing\RouteRunner 的实例,而我显然给了它 Psr\Http\Message\ResponseInterface任何的想法?编辑:感谢delboy的回答,但你能更具体点吗?苗条的文档显示像这样使用它(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;});但这不起作用!总是得到类型错误,那么他们的文档是否过时了?如果是这样,我如何在这里实现中间件?我的示例代码传递了 3 个参数,而不是 2 个!
查看完整描述

3 回答

?
MM们

TA贡献1886条经验 获得超2个赞

您的中间件没有实现 PSR-15。您不应该传递响应,而是传递请求处理程序接口:


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;

}

https://www.php-fig.org/psr/psr-15/


查看完整回答
反对 回复 2021-12-24
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

需要添加:使用Slim\Psr7\Response;


查看完整回答
反对 回复 2021-12-24
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

首先,您需要检查您使用的是哪个超薄版本,当我开始学习时,我也遇到了这个错误/问题,但您想解决这个问题,您可以在您的代码中进行此编辑。


$APP = AppFactory::create();

$app->setBasePath("/myapp/public/index.php");


$APP->add(function(Request $request, Response $response, callable $next){

    $response = $next($request,$response);

    return $response;

});


查看完整回答
反对 回复 2021-12-24
  • 3 回答
  • 0 关注
  • 185 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号