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.

Question author Adjenks | Source



You can't solve that with autowiring only. You have to write a bit of configuration to inject your array into the classes you need to: http://php-di.org/doc/php-definitions.html

See also here to read how to set up a configuration file with Slim and PHP-DI: http://php-di.org/doc/frameworks/slim.html#configuring-php-di

Answer author Adjenks

Ask about this question here!