My Slim 3 application index structure is:
// Instantiate the app
$config = require __DIR__ . '/../src/config.php';
$app = new \Slim\App($config);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
I have separate worker.php file which is executed through bash script and this file running non-stop in background ( listens for some events).
How can I inject services in my worker.php file which is not part of Slim app or $container from dependencies.php , for example i want to use Monolog logger. dependencies:
$container = $app->getContainer();
/**@var \App\model\Model access to Model methods */
$model = new \App\model\Loan($app->getContainer()->get('settings')['db']);
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new Monolog\Logger($settings['name']);
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
$logger->pushHandler(new Monolog\Handler\StreamHandler($settings['path'], Monolog\Logger::DEBUG));
return $logger;
};
Easiest way is to use the same DI Container in your worker.php
Reference: https://github.com/slimphp/Slim/blob/3.x/Slim/Container.php
$config = require __DIR__ . '/../src/config.php';
$container = new \Slim\Container($config);
require __DIR__ . '/../src/dependencies.php';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With