adjenks adjenks - 9 days ago 5
PHP Question

Adding one regular variable to auto-wired function

I'm using Slim Framework with PHP-DI to autowire dependencies for me. But one dependency is just a regular array. If I put a regular array into my container configuration, then all arrays will be set to that one array. So my primary question would be:

How do I inject just one single variable, while letting the container auto-wire the rest? Is this possible? I've found myself writing a route like this:

$app->get('/userConfig', function (
Request $request,
Response $response,
Preferences $prefs,
UserConfig $userconfig)
{
$myArray = ['Thing1','thing2','thing3'];
return $userconfig->configView($request, $response, $myArray, $prefs);
});


Whereas all my other routes are short like this, because they only have dependencies on unique classes:

$app->get('/testPage', ['\Test','myTestPage']);


I wrote all that extra stuff just to squeeze
$myArray
into the configView function, is there a way to combine regular dependency injection with autowiring? Does any framework or library do that?

I could have just written it like this if I didn't need that one array:

$app->get('/userConfig', ['\UserConfig','configView']);


Alternatively, I could reach into the container and get the array, but that would make the page-function dependent on the container, which is something which should be avoided.

Answer

I needed to use the call() function of the container. As you can see in the question there is also a "Preferences" parameter for configView, however I did not have to pass it, the container did that for me. I didn't understand how to pass it partial sets of parameters, this is how, as an array in the second parameter of call() like so:

$app->get('/userConfig', function (
        Request $request, 
        Response $response
    ){
        $myArray = ['Thing1','thing2','thing3'];
        return $this->call(['UserConfig', 'configView'],[$request,$response,$myArray]);
    });

The key part being [$request,$response,$myArray] and not requiring EVERY OTHER parameter. In my example there is only one, but my actual object had 5 more, I wanted to know how to avoid dealing with the other parameters, this was how...