PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

Feature request: Instantiate injection with route placeholder value #53

Closed danielsetreus closed 4 years ago

danielsetreus commented 4 years ago

Consider this:

index.php

$this->app->get('/foo[/{fromDate}]', [FooController::class, 'bar']);

FooController.php

public function bar(Request $request, Response $response, DateTimeDto $fromDate = null) {
   ...
}

DateTimeDto.php

class DateTimeDto {
    public function __construct(string $someTime = null) {
        ...
    }
}

Would it today be possible to inject DateTimeDto into barMethod constructing it with the optional value in the route? I.e. GET /foo/2019-01-01 would inject new DateTimeDto('2019-01-01')

In essence what I want is to automatically cast a placeholder value to an arbitrary type. For all I know it works when casting the value to a int, but not with dependency injections.

mnapoli commented 4 years ago

That is not something I plan on implementing: it is out of the scope of the container (those are route parameters, not container services/parameters) and we are approaching the magic of full-fledge frameworks like Laravel/Symfony.

I mean, the feature makes sense, but to me it's a feature that could be implemented in a 3rd party package.