0

I'm using the PHP Slim Framework V4 to build an API with php-di/slim-bridge to have dependency injection within the controller methods.

What's the best practice when working with DTOs for incoming requests in Slim Framework v4?

I'd like to use DTOs to map and validate request parameters. I could do:

class TestController
{


    public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
    {
        $myDto = new BaseDto($request);

        {...}

        return $response;
    }

}

However I'd like to archive this by directly injecting the DTO into the Controller:

class TestController{

    public function index(BaseDto $myDto): ResponseInterface
    {
        {...}

        return $response;
    }

}

But because as of Slim v4, the request is no longer part of the DI container, the dependency of the BaseDto cannot be resolved and I get the error:
cannot be resolved: Entry "Psr\Http\Message\ServerRequestInterface" cannot be resolved: the class is not instantiable

CRaSH3k
  • 11
  • 1
  • I would recommend to not use slim-bridge. Instead just read the json or form-data or the query-parameters from the request object, then validate this data and then create a valid DTO from it. – odan Apr 20 '23 at 11:29

0 Answers0