phpmysqlerror-handlingslimslim-3

Log PHP exceptions and Errors to Database table PHP Slim 3


So I am trying to find the best way to get PHP SLIM 3 to log PHP exceptions and errors to a Database table instead of a log. whats the best way to accomplish this? So far in the documentation recommends this.

Dependency:

$app = new \Slim\App();
$c = $app->getContainer();
$c['errorHandler'] = function ($c) {
    return function ($request, $response, $exception) use ($c) {
        return $response->withStatus(500)
            ->withHeader('Content-Type', 'text/html')
            ->write('Something went wrong!');
    };
};

What I am having trouble understanding is where do I call this and how could I connect this to log it into a database table that I created. Any Ideas?


Solution

  • Have you tried this for Slim 3?

    
    $config = [
        'settings' => [
            'displayErrorDetails' => true,
    
            'db' => [
                'driver' => 'mysql',
                'host' => 'localhost',
                'username' => 'root',
                'database' => 'test',
                'password' => '',
                'charset' => 'utf8mb4',
                'collation' => 'utf8mb4_unicode_ci',
               'flags' => [
                   PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
               ],
            ]
        ],
    ];
    
    $app = new \Slim\App($config);
    
    $container = $app->getContainer();
    
    $container[PDO::class] = function ($container) {
        $settings = $container['settings']['db'];
    
        $host = $settings['host'];
        $dbname = $settings['database'];
        $username = $settings['username'];
        $password = $settings['password'];
        $charset = $settings['charset'];
        $flags = $settings['flags'];
        $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
    
        return new PDO($dsn, $username, $password, $flags);
    }
    
    $container[PDO::class] = function ($container) {
        $settings = $container['settings']['db'];
    
        $host = $settings['host'];
        $dbname = $settings['database'];
        $username = $settings['username'];
        $password = $settings['password'];
        $charset = $settings['charset'];
        $flags = $settings['flags'];
        $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
    
        return new PDO($dsn, $username, $password, $flags);
    };
    
    $container ['errorHandler'] = function ($container ) {
        $connection = container[PDO::class];
    
        return function ($request, $response, $exception) use ($connection) {
    
            $row = [
                'message' => $exception->getMessage(),
            ];
    
            $sql = "INSERT INTO error_logs SET message=:message";
    
            $connection->prepare($sql)->execute($row);
    
            return $response->withStatus(500)
                ->withHeader('Content-Type', 'text/html')
                ->write('Something went wrong!');
        };
    };