Search code examples
phpslimmiddleware

Getting error when adding a middleware to the route in Slim


I wrote this code in Slim and it works fine but when I add the middleware I get the following error!!! Can not figure out what is happening, could any one please help me.

PHP Catchable fatal error:  Argument 3 passed to fileFilter() must be callable, array given FILENAME in line 90

This middleware filters unsupported file types

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
    }

And here I add the middleware to my route.

 $app->group('/test/api/v1', function () {
        // other routes here
        $this->post('/resume/edit','fileFilter', ResumeController::class. ':edit')->setName('Resume.edit');


    });

Solution

  • You should remove 'fileFilter' from

    $this->post('/resume/edit', ...
    

    and change it to something like

    $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
    });
    

    or as invokable 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
        }
    }
    

    and in route

    $this->post(...)->add(MyMiddleware::class);
    

    Slim Middleware