Hi,
I have just three questions about good practice with container DI.
First question:
If in a class constructor, I instancie directly a class (simple class without parameters, see bellow) which are not created in my container DI definition, I see it’s working (because of autowiring I think), but it’s preferable do declare them in container DI definition or not needed?
use App\Models\UserAuth;
class MyClass
{
private $userAuth;
public function __construct(UserAuth $userAuth)
{
$this->userAuth= $userAuth;
}
Second question:
I have multiple Controller class (HomeController, LoginController, etc …) (which doesn’t have constructor) and extends a ParentController like this:
use App\Controllers\ParentController;
class LoginController extends ParentController
{
...
}
My ParentController has multiple class in the constructor (accessible for all my Container Files) and directly injected with container DI autowiring:
<?php
namespace App\Controllers;
use App\Helpers\Flash;
use Slim\Views\PhpRenderer;
class ParentController
{
public $settings;
private $phpView;
public $flash;
public function __construct(array $settings, PhpRenderer $phpView, Flash $flash)
{
$this->settings = $settings;
$this->phpView = $phpView;
$this->flash = $flash;
}
...
My “settings” is create in container DI definitions, but it’s not a class, so I can’t instanciate them with autowiring (automatically) in my constructor, so the only way I have found for inject them to my ParentController of all my Controller files (HomeController, LoginController, etc …) is to do this (see bellow) in container DI definitions (with the wildcard * for select all Controller files), I have the right method ?
return [
'settings' => function()
{
return require __DIR__.'/settings.php';
},
'App\Controllers\*Controller' => autowire()->constructorParameter('settings', get('settings')),
];
Third question:
It’s about to create a constructor in one of my child Controller, for example “LoginController” (which extend parentController). If I want to inject a class “UserAuth” like this:
use App\Models\UserAuth;
use App\Controllers\ParentController;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class LoginController extends ParentController
{
private $userAuth;
public function __construct(UserAuth $userAuth)
{
$this->userAuth = $userAuth;
parent::__construct();
}
...
I can’t because my parent::__construct()
also need the parameters create in “ParentController”, so the only way I have found it’s to call again all parameters like this bellow, I have the right method ?
use App\Helpers\Flash;
use App\Models\UserAuth;
use Slim\Views\PhpRenderer;
use App\Controllers\ParentController;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class LoginController extends ParentController
{
private $userAuth;
public function __construct(array $settings, PhpRenderer $phpView, Flash $flash, UserAuth $userAuth)
{
$this->userAuth = $userAuth;
parent::__construct($settings, $phpView, $flash);
}
...
If you can help me to see better with my three question, thanks a lot