Logger in router.php - How to

Hello,

there has been a lot of time questions on how to use logger (Monolit) in the router.php file, that comes out the box when running slim from Docker, under shinsenter/slim:latest image.

The answers on the web are sometimes confusing and sometimes pointing to different versions. After reading and doing some trial and error, here is my fast and dirty solution to this problem.

<?php

declare(strict_types=1);

use App\Application\Actions\DB\DB;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
use Psr\Log\LoggerInterface;

return function (App $app) {

    $container = $app->getContainer();
    $logger = $container->get(LoggerInterface::class);

    $app->get('/', function (Request $request, Response $response) use ($logger) {
        $response->getBody()->write('Hello world!');

        $logger->info("/ GET was called.");

        return $response;
    });

when using RouteCollectorProxyInterface as $group, you can use this:

<?php

declare(strict_types=1);

use App\Application\Actions\DB\DB;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;
use Psr\Log\LoggerInterface;

return function (App $app) {

    $app->group('/api', function (Group $group) use ($app) {

        $container = $app->getContainer();
        $logger = $container->get(LoggerInterface::class);

        $group->options('/{routes:.*}', function (Request $request, Response $response) {
            // CORS Pre-Flight OPTIONS Request Handler
            return $response;
        });

        $group->get('/', function (Request $request, Response $response) use ($logger) {
            $response->getBody()->write('Hello world!');

            $logger->info("/api/ GET was called");

            return $response;
        });

I just hope it helps someone, as it took me some time to figure this out.

Thanks for sharing this. Note that it is also possible to fetch dependencies directly within the route callback.

$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello world!');

    $logger = $this->get(LoggerInterface::class);
    $logger->info("/ GET was called.");

    return $response;
});
1 Like

Hi,
sorry to coming to this after some months, but is there a way to call loggerInterface from a PHPMailer Support Class, like presented in this post: PHPMailer Example?

I tried your initial injection, and it didn’t work. I must say, that this injections stuff is not very intuitive to me.

Thx

I tried your initial injection, and it didn’t work.

Sorry to hear that. Could you share more details?

I must say, that this injections stuff is not very intuitive to me.

The concept of dependency injection is to declare all required classes and interfaces within the class constructor. The DI container will then create and inject these objects. It’s a straightforward and efficient way to manage object dependencies.

Using functions (closures) as routing handlers means that you cannot benefit from the concept of DI. The non-class based approach (closures as routing handler) can become unmaintainable in the long run as you have to manually create, wire, and pass everything together.