あなたはミドルウェアから1つのルートを除外するために必要がある場合は、2つのオプションがあります。
オプション1:グループあなたのルートは。
あなたはグループ1を除くすべてのルートすることができます
<?php
$app->group('', function() {
// All routes declarations....
})->add($container->csrf); // Add middleware to all routes within the group
// Declare your "exceptional" route outside the group
$app->post('my-special-route-that-has-no-csrf-middleware', 'routeProcessor');
オプション2:代わりに、直接\Slim\Csrf\Guard
を使用する独自のミドルウェア
を使用することは、それを拡張して、独自のミドルウェアを使用します。あなたのミドルウェアはルートをチェックし、ルートが "例外的な"ものであればスキップします。 orginial \Slim\Csrf\Guard
拡張ミドルウェアを作成
$container['settings'] => [
'determineRouteBeforeAppMiddleware' => true
];
:
<?php
class MyCsrfMiddleware extends Slim\Csrf\Guard
{
// This method is processing every request in your application
public function processRequest($request, $response, $next) {
// Check if it's your "exceptional" route
$route = $request->getAttribute('route');
if ($route == 'my-special-path') {
// If it is - just pass request-response to the next callable in chain
return $next($request, $response);
} else {
// else apply __invoke method that you've inherited from \Slim\Csrf\Guard
return $this($request, $response, $next);
}
}
}
/////////////
$container['csrf'] = function($container) {
return new MyCsrfMiddleware; // Now the container returns your middleware under 'csrf' key
};
今単に\Slim\App
インスタンスにミドルウェアを追加します。 あなたがミドルウェア内のルートにアクセスする必要があるため
は、設定にこれを追加します
$app->add('csrf:processRequest');