向 Slim 中的路由添加中间件时出错
Getting error when adding a middleware to the route in Slim
我在 Slim 中编写了这段代码,它运行良好,但是当我添加中间件时,出现以下错误!!!无法弄清楚发生了什么,任何人都可以帮助我。
PHP Catchable fatal error: Argument 3 passed to fileFilter() must be callable, array given FILENAME in line 90
此中间件过滤不支持的文件类型
use Slim\Http\Request;
use Slim\Http\Response;
use Api\ErrorList as ErrorList;
function fileFilter(Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
}
在这里我将中间件添加到我的路由中。
$app->group('/test/api/v1', function () {
// other routes here
$this->post('/resume/edit','fileFilter', ResumeController::class. ':edit')->setName('Resume.edit');
});
您应该从
中删除 'fileFilter'
$this->post('/resume/edit', ...
并将其更改为类似
的内容
$this->post(...)->add((Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
});
或作为可调用的 class
class MyMiddleware
{
public function __invoke(Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
}
}
在路线
$this->post(...)->add(MyMiddleware::class);
我在 Slim 中编写了这段代码,它运行良好,但是当我添加中间件时,出现以下错误!!!无法弄清楚发生了什么,任何人都可以帮助我。
PHP Catchable fatal error: Argument 3 passed to fileFilter() must be callable, array given FILENAME in line 90
此中间件过滤不支持的文件类型
use Slim\Http\Request;
use Slim\Http\Response;
use Api\ErrorList as ErrorList;
function fileFilter(Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
}
在这里我将中间件添加到我的路由中。
$app->group('/test/api/v1', function () {
// other routes here
$this->post('/resume/edit','fileFilter', ResumeController::class. ':edit')->setName('Resume.edit');
});
您应该从
中删除 'fileFilter'$this->post('/resume/edit', ...
并将其更改为类似
的内容$this->post(...)->add((Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
});
或作为可调用的 class
class MyMiddleware
{
public function __invoke(Request $request, Response $response, callable $next){
$allowedFiles = ['image/jpeg', 'image/png', 'application/pdf'];
$files = $request->getUploadedFiles();
$flattened =array_flatten($files);
foreach ($flattened as $key=> $newFile){
$newFileType = $newFile->getClientMediaType();
if(!in_array($newFileType, $allowedFiles)) {
return ResponseHelper::createfailureResponse($response, HTTPStatusCodes::BAD_REQUEST, ErrorList::UNSUPPORTED_FILE_TYPE);
}
}
return $next($request, $response); // line 90
}
}
在路线
$this->post(...)->add(MyMiddleware::class);