【发布时间】:2019-07-22 06:50:05
【问题描述】:
我在 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');
});
【问题讨论】:
标签: php slim middleware