I am currently working on a slim 3 application that is using a php-di bridge to serve dependencies. Everything is working flawlessly within controllers, however, I came across an issue when trying to add dependencies to a model observer. Eloquents capsule manager has its own container so I am unable to pass through dependencies (not injected) without it throwing an error.
Current setup:
$app = new \App\App; // extends \DI\Bridge\Slim\App
$container = $app->getContainer();
$capsule = new Capsule;
$capsule->addConnection($settings['settings']['db']);
$capsule->setEventDispatcher(new Dispatcher());
$capsule->setAsGlobal();
$capsule->bootEloquent();
// Sale model extends eloquent model
Sale::observe(new SaleObserver($container));
SaleObserver Class:
class SaleObserver {
protected $container;
public function __construct($container){
$this->container = $container;
}
public function saving(Sale $sale){
// Mail logic using container
}
}
This gives me the error triggered from my SaleObserver:
Unresolvable dependency resolving [Parameter #0 [ <required> $container ]]
I think the problem is that the SaleObserver is being resolved my eloquent's own container which is not allowing me to pass my PHP-DI "$container" through. Is something like this possible within slim 3 without a hacky approach?
I am testing with container just to see if I can pass something through, however, my main objective is to simply pass through my mail definition since it has all the configs already setup.
Update:
I have previously tried to type hint within the SaleObserver class like below in hopes that php-di would catch it with no avail.
use App\Mail\Contracts\MailerInterface;
class SaleObserver {
protected $mail;
public function __construct(MailerInterface $mail){
$this->mail = $mail;
}
public function updating(Sale $sale){
// Send Mail logic
}
}
I end up getting a different error that suggests that php-di auto wiring is not working with classes that are not not connected to a route even though auto wiring is set to true because the injected parameter is not automatically instantiating as it should.
This shows the error:
Uncaught ArgumentCountError: Too few arguments to function
The bridge I am using is PHP-DI/Slim-Bridge