Problem: DI Factories passing entire container


Hi, I’m a bit new to frameworks and DI in general but I found this article helpful:

But I’m having a problem, it seems that when I try to inject a dependency into a controller it’s getting passed the entire container object.

 $container['DatabaseService'] = function ($c) {
    return new \Lib\SqlModel(); // Aura/Sql wrapper

$container['ServiceRecordsController'] = function($c) {
    $dbService = $c->get('DatabaseService');
    return new ServiceRecordsController($dbService);

My route looks like so:

$app->get('/records', ServiceRecordsController::class . ':show')

And my controller looks like:

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.

What am I missing?

Thanks - Don


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.

$app->get('/records', \ServiceRecordsController::class . ':show')

Is yoru controller class namespaced?


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.

Thanks, Don


Check if that contains a reference to the container.


Try to namespace the container ID e.g.

$container[ServiceRecordsController::class] = function($c) {


If your class is namespaced, your container entry will likely need to be.

$app->get('/records', App\Controllers\ServiceRecordsController::class . ':show')


Thanks, I’ll check this out tonight. This is on a personal project.