Erez Hod
Erez Hod

Reputation: 1883

Slim\\Route::__invoke() with Middleware is not accepting the right $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!

Upvotes: 0

Views: 3769

Answers (1)

Erez Hod
Erez Hod

Reputation: 1883

Stupidly.. I noticed that on

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

I reversed the parameters.. should be

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

Blaahh... my head hurts.

Upvotes: 4

Related Questions