Controller constructor not being called by Dependencies what gives?

So I’ve setup the dependencies.php file with the following call:

//class definitions
$container[‘MyNewController’] = function($c){

return new MyNewController($c->get('settings')['db'], $c->get('renderer'));


When I access the route like so:

$app->get(’/newcontroller/ths/home’, ‘NewController::home’);

the correct action is called without a problem. That being said, the constructor never gets called so I cannot access the keys from the container in the dependencies file. So two errors I see, adding the NewController class to the container / dependencies file does not init the constructor on the NewController class AND I can’t access my private vars via the $this operator, what gives??

class NewController{

private $rdb;
private $view;

__construct($rdb, $view){

 $this->rdb = $rdb;
 $this->view = $view;


public function home($request, $response){ //do stuff here not a problem }


I assume this is pseudo code and not exactly what you have? You have an entry for MyNewController but your class is NewController. I’d think you might want to consider the visibility of your properties and your constructor as well.

class NewController { // or MyNewController ?

    protected $rdb;
    protected $view;

    public function __construct($rdb, $view){
        $this->rdb = $rdb;
        $this->view = $view;

    public function home($request, $response){
         //do stuff here not a problem

Use a single : in

$app->get('/newcontroller/ths/home', 'NewController:home');
1 Like

Ahhh… good eye. Missed that.

yes guys this was pseudo…my bad …and yes I’m using :: on the app->get call. And yes I renamed the controller name as well. Another typo…oops! Matches in the real code though…

But any ideas though as to why the constructor doesn’t get called? The action works fine

Here’s the full code so that you guys can see the real deal…

<?php use Slim\Http\Request; use Slim\Http\Response; use Slim\Container; class MyNewController { private $rdb; private $view; function __construct($rdb, $view) { $this->rdb = $rdb; $this->view = $view; } #home action public function home(Request $request, Response $response){ echo 'hello home!'; var_dump($this->rdb); #fails return; } } #routes.php $app->get('/mynewcontroller/ths/home', 'MyNewController::home'); #dependencies.php $container['MyNewController::class'] = function($c){ return new MyNewController($c->get('settings')['db'], $c->get('renderer')); }; Again action works fine , but constructor is not called and references **$this** in action fails

As the amazing @akrabat pointed out, try a single colon:

$app->get('/mynewcontroller/ths/home', 'MyNewController:home');

And I believe in your container you just want the class name:

$container['MyNewController'] = function ($c) {

See Container Resolution from the docs for more examples.

You have to use a single colon. A double colon is interpreted by PHP as a static method call which is why $this doesn’t work.

Thank you @akrabat! I’ll try that shortly and see if that fixes it for me. But definitely sounds like that’s the culprit! Good to know how the framework is interpreting the route class definitions! :slight_smile:

thanks again,

@tflight , it was late for me yesterday and not sure why I just glossed over your comment above about the single colon…thanks though for pointing that out. Repetition is always best :slight_smile: and thanks to @akrabat again… i’m now off to the races!

Go slim! :slight_smile: