1
現在、Slim Framework 3を使用し、基本認証用にミドルウェアを実装しているREST APIを作成しています。
私のルーティングは次のようなものです。私は、次のように__invoke方法使用していますAuthMiddlewareクラスで
$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));
});
});
:
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;
}
}
をそして、私は次のエラーを取得しています:
Argument 1 passed to Slim\Route::__invoke() must implement interface Psr\Http\Message\ServerRequestInterface, instance of Slim\Http\Response given
次の行:
$response = $next($response, $request);
何が起こっているか何か案は?私は2時間:(
おかげでたくさんのために、この上に自分を食べてきた!
スリムなバージョンを混在させているようです。vのルートのメソッドシグネチャあなたがちょうど '$ this'を使っているグループの中で' $ app-> get( '/ path'、function(Request $ request、Response $ response){...}); (アプリ) '。また、スリムの設定で 'pdo' varをコンテナに追加して、次のようにアクセスすることができます:' $ this-> pdo-> methodName(); ' – nerdlyist
@nerdlyistに感謝します。しかし、 PHPStormは単にpdoメソッドの自動補完を停止します。 – EpicSyntax
彼らが奇妙に思えるかもしれないが、多少のサポートが得られるかもしれませんが、私はあなたのシステムアーキテクチャーをそれに基づいて推測することは勧めません。たとえば、膨れ上がったインデックスファイルを介してコントローラに移動したい場合など、ルート上のコンテナ内のコンテナ内に多くの利点があります。 – nerdlyist