0

Hi I have the follwong dependencies:

$container['controller'] = function($c) use ($app) {
    return new controller( $app );
};
$container['utils'] = function($c) use ($app) {
    return new utils( $app );
};
$container['model'] = function($c) use ($app) {
    return new model( $app );
};

from controller class I call to MODEL calss like :

$this->continer->model->function() . 

this is works perfect , but now from the model I cannot call to "utils" class or other class

$this->continer->utils->function() . 

and got :

Notice: Undefined property: Slim\App::$model

Pravitha V
  • 3,308
  • 4
  • 33
  • 51
fstdv
  • 53
  • 1
  • 9
  • This might help? https://stackoverflow.com/questions/40176059/dependency-injection-in-slim-framework-passing-container-into-your-own-classes – Andy Aug 03 '17 at 11:36
  • Working for 1 level class but for the second class I don't manage to call to the third class with container – fstdv Aug 03 '17 at 11:44
  • Ok, I'm not sure what else to suggest but feel the problem is along the same lines as what I posted in the above link. I only used Slim for 1 project and then stopped using it precisely because of that problem. It's really tedious to have to manually dependency inject things all over. The accepted answer on that post involved using a different dependency injector, which does make things better, but it's still light years behind something like CakePHP's way of doing things. Sorry I can't help further. The person who answered my post knows a lot about Slim so maybe contact them if you can? – Andy Aug 03 '17 at 11:51
  • You are injecting the whole app incl. the container. It would be better to just pass the required dependencies for the specific object via constructor injection. – odan Aug 03 '17 at 11:59

1 Answers1

0

Found the error should inject the container instead $app

$container['model'] = function($c) use ($app) {
    return new model( $c );
};
fstdv
  • 53
  • 1
  • 9