Abstract class Dependency Injection


#1

I’m using an abstract base controller:

class ProductController extends Controller{}

I’m trying to get the container injected into the constructor, but having no luck. I’m using factories for the standard controllers for DI which works and I would be happy to only inject what I need, but it’s an abstract class so as Slim injects the container by default I thought that would be the solution but it’s not working.

I’m using psr-4 and my Controllers folder is under my app folder.

“autoload”: {
“psr-4”: {
“App\”: “app”,
“Tests\”: “tests”
}

namespace App\Controllers;

abstract class Controller
{
protected $logger;

/**
 * Controller constructor.
 */

public function __construct($container) 
{
    $this->logger = $container->get('Logger');
}

public function test()
{
   $this->logger->info('test');
}

}

Monolog is in the container and I can use it using normal DI in standard controllers.

Thanks,

Lee.