Slim3 + eloquent observe + redis


#1

Hello. Help me please…
i want setup event in model for redis cache update when data save
i use native slim3 container but for observe i use Illuminate\Container\Container
becouse i have problem like this https://stackoverflow.com/questions/51506326/is-it-possible-to-use-php-di-definitions-instead-of-eloquents-capsule-managers-b

$container['laravel-container'] = function ($c){
    return new \Illuminate\Container\Container;
};
$container['database-event-dispatcher'] = function ($c) {
    return new \Illuminate\Events\Dispatcher($c['laravel-container']);
};
$capsule->setEventDispatcher($container['database-event-dispatcher'])

next

Dappur\Model\RoomUser::observe(new Dappur\Model\Observers\RoomUserObserver($container));

namespace Dappur\Model\Observers;
use Dappur\Model\RoomUser;
use Interop\Container\ContainerInterface;
use Illuminate\Container\Container;
use Symfony\Component\Cache\Adapter\RedisAdapter;
class RoomUserObserver {
    public $container;
    public function __construct($container) {
        $this->container= $container;		
    }
    public function saved(RoomUser $roomUser) {
    }
}

and i have error

/var/www/roombear/data/www/roombear.ru/vendor/illuminate/container/Container.php
Line: 933
Message: Unresolvable dependency resolving [Parameter #0 [ $container ]] in class Dappur\Model\Observers\RoomUserObserver

where could i make a mistake?
thank you.


#2

Maybe you should give the dependency resolver the information what he has to resolve. For example add Container $container to the constructor (instead of mixed):

use Illuminate\Container\Container;
// ...
public function __construct(Container $container) {

}

#3

I have already done so) did not help)
The second day I can not understand why it does not work.)))


#4

if anyone is interested in how I solved the problem.

class RoomObserver {

    static $container = null;

    public function __construct($container = null) {
	
		if(is_null(self::$container)){
			self::$container = $container;			
		}
    }
    public function saved(Room $room) {
    }
}