Hi,
I’m enjoying using Slim. I opted to take the advice to plunge straight in and use the skeleton.
Because I don’t know how things are connected together in the Container
, I’m having difficulty knowing how to access classes that have been injected elsewhere.
For e.g., I’m writing a simple action that takes place in routes:
routes.php
...
$app->get('/gallery', function (Request $request, Response $response) {
$gallery = array_filter(glob(__DIR__ . '/../public/gallery_images/thumbs/*'), 'is_file');
$view = Twig::fromRequest($request);
return $view->render($response, "gallery.twig", [
'gallery' => $gallery,
]);
})->setName('gallery');
How do I access the Logger that has already been injected into the DI Container within that route?
I’ve tried:
$this->logger->debug("Message here")
ERROR: Undefined property: DI\\Container::$logger
and
$logger = $this->get('logger')
"No entry or class found for 'logger'"
but no success.
The settings.php
file is, apart from the name
change and setting the log values to true
is as it was from installation:
use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;
return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
SettingsInterface::class => function () {
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logError' => true,
'logErrorDetails' => true,
'logger' => [
'name' => 'my_app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
]);
}
]);
};
Can anyone help?