Get settings in slim 4 (eg. path of application root directory)

846 Views Asked by At

Question

How can I access the path settings, or more general, how do I access settings defined in the settings.php.

My settings.php

Important note: These are the values for the test environment, they have to be changed for production!

// Should be set to 0 in production
error_reporting(E_ALL);

// Should be set to '0' in production
ini_set('display_errors', '1');

// Timezone
date_default_timezone_set('FILL_IN_YOUR_TIMEZONE');

// Settings
$settings = [];

// Path settings
$settings['root'] = dirname(__DIR__);
$settings['temp'] = $settings['root'] . '/tmp';
$settings['public'] = $settings['root'] . '/public_html';

// Error Handling Middleware settings
$settings['error'] = [

    // Should be set to false in production
    'display_error_details' => true,

    // Parameter is passed to the default ErrorHandler
    // View in rendered output by enabling the "displayErrorDetails" setting.
    // For the console and unit tests we also disable it
    'log_errors' => true,

    // Display error details in error log
    'log_error_details' => true,
];

return $settings;

I've been working on this for hours, trying different thinks, but neither found any working clue in the Slim v4 documentation, nor on other sites.

1

There are 1 best solutions below

0
On BEST ANSWER

The solution is pretty easy, but somehow there is a lack of documentation for slim 4:

You can access the settings via $app->get('settings'). Within a route, that is $this, of course

For example:

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$app->get('/wiki/{id:[1-9]\d*}/edit'), function (ServerRequestInterface $request, ResponseInterface $response) {
   ...
   $filename = $this->get('settings')['root'].'/src/content/wiki/edit.html';
   ...
   return (...);
});