使用Middleware的Slim \\ Route :: __ invoke()不接受正确的$ request和$ response

I am currently writing a REST API using Slim Framework 3 and implementing Middleware for basic authentication.

My routing goes like this:

$app->group('/api', function () use ($app, $pdo) {
    $this->group('/v1', function () use ($app, $pdo) {

        // Guest Routes
        $this->group('', function() use ($app, $pdo) {
            require_once '../app/api/v1/authentication.php';
        });

        // Authenticated Routes
        $this->group('', function() use ($app, $pdo) {
            require_once '../app/api/v1/test.php';
        })->add(new \App\Middleware\AuthMiddleware($pdo));

    });
});

In the AuthMiddleware class I am using the __invoke method in the following way:

namespace App\Middleware;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

class AuthMiddleware extends Middleware {

/**
 * @param Request $request
 * @param Response $response
 * @param $next
 * @return Response
 */
    public function __invoke(Request $request, Response $response, $next) {

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

        return $response;
    }

}

And I'm getting the following error:

Argument 1 passed to Slim\Route::__invoke() must implement interface Psr\Http\Message\ServerRequestInterface, instance of Slim\Http\Response given

on the following line:

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

What is happening? any ideas? I've been eating myself over this for 2 hours :(

Thanks a bunch!

douying0108
douying0108 嗯......这对他们来说似乎很奇怪可能得到一些支持,但我不建议将你的系统架构基于此。例如,如果要通过膨胀的索引文件移动到控制器,它会在路径中的容器中添加很多好处。
大约 4 年之前 回复
dpy15285
dpy15285 谢谢@nerdlyist,但是当我将pdo添加到容器PHPStorm时,只是停止了pdo方法的自动完成。
大约 4 年之前 回复
dtn913117
dtn913117 看起来你正在混合超薄版本。版本3中路由的方法签名是$app->get('/path',function(Request$request,Response$response){...});在一个组中你只需使用$this而不需要使用(app)。同样在你的slim设置中,你可以将pdovar添加到容器中,然后像下面这样访问它:$this->pdo->methodName();
大约 4 年之前 回复

1个回答



愚蠢..我注意到了</ p>

  $ response = $ next($ 响应,$ request); 
</ code> </ pre>

我颠倒了参数..应该是</ p>

  $ response = $ next  ($ request,$ response); 
</ code> </ pre>

Blaahh ......我的头疼。</ p>
</ div>

展开原文

原文

Stupidly.. I noticed that on

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

I reversed the parameters.. should be

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

Blaahh... my head hurts.

dongwei3336
dongwei3336 我感觉到你的痛苦!
一年多之前 回复
doudengshen5591
doudengshen5591 霍莉......哈哈哈哈,我犯同样的错误......
一年多之前 回复
立即提问
相关内容推荐