...
public function __construct($dbService)
{
$this->dbService = $dbService; // this is the container, not $c['DatabaseService']
$this->serRecHandle = new ServiceRecords($this->dbService);
}
...
when I eventually get to where I use $dbService I need to reference it as $dbService[‘DatabaseService’] and it works fine.
If Slim doesn’t fid an entry for your controller in the container, it will pass the container to the controller’s constructor. So it would appear as though Slim isn’t finding the entry for your controller in the container if you are getting it in the constructor.
The issue isn’t obvious to me based on what you have posted. I wonder if it is perhaps a namespace issue, or perhaps needing a leading backlash.
Yes it’s namespaced, I tried the leading slash and the app broke. Now that I know why it’s acting like it is I’ll work with it and see what I can find.
Please excuse my bumping this old thread but, … to which result did you come @dstefani ?
I’m asking this because I have exactly the same problem, and am still trying to find the solution.
Oh my goodness,… literally 2 minutes after this post, I figured out the cause by having a look at an example API by @akrabat.
The original code :
$container['FileServiceController'] = function ($container) {
return new App\Controllers\FileService\FileServiceController($container['settings']['FileStoragePath']);
};
The solution was to not add to container via string identifier, but to use the Class:class :
$container[App\Controllers\FileService\FileServiceController::class] = function ($container) {
return new App\Controllers\FileService\FileServiceController($container['settings']['FileStoragePath']);
};
Presumed you are using the Classname::class.’:method’ approach in your routes. It seems that whatever pattern you use in your routes, has to be used throughout the project, or more precisely the other way round.
At least that does work for now, but probably introduces other issues.