Application

The Application, (or Slim\App) is the entry point to your Slim application and is used to register the routes that link to your callbacks or controllers.

  1. // instantiate the App object
  2. $app = new \Slim\App();
  3. // Add route callbacks
  4. $app->get('/', function ($request, $response, $args) {
  5. return $response->withStatus(200)->write('Hello World!');
  6. });
  7. // Run application
  8. $app->run();

Application Configuration

The Application accepts just one argument. This can be either a Container instance oran array to configure the default container that is created automatically.

There are also a number of settings that are used by Slim. These are stored in the settingsconfiguration key. You can also add your application-specific settings.

For example, we can set the Slim setting displayErrorDetails to true and also configureMonolog like this:

  1. $config = [
  2. 'settings' => [
  3. 'displayErrorDetails' => true,
  4. 'logger' => [
  5. 'name' => 'slim-app',
  6. 'level' => Monolog\Logger::DEBUG,
  7. 'path' => __DIR__ . '/../logs/app.log',
  8. ],
  9. ],
  10. ];
  11. $app = new \Slim\App($config);

Retrieving Settings

As the settings are stored in the DI container so you can access them via the settings key in container factories. For example:

  1. $loggerSettings = $container->get('settings')['logger'];

You can also access them in route callables via $this:

  1. $app->get('/', function ($request, $response, $args) {
  2. $loggerSettings = $this->get('settings')['logger'];
  3. // ...
  4. });

Updating Settings

If you need to add or update settings stored in the DI container after the container is initialized,you can use the replace method on the settings container. For example:

  1. $settings = $container->get('settings');
  2. $settings->replace([
  3. 'displayErrorDetails' => true,
  4. 'determineRouteBeforeAppMiddleware' => true,
  5. ]);

Slim Default Settings

Slim has the following default settings that you can override:

  • httpVersion
  • The protocol version used by the Response object. (Default: '1.1')
  • responseChunkSize
  • Size of each chunk read from the Response body when sending to the browser. (Default: 4096)
  • outputBuffering
  • If false, then no output buffering is enabled. If 'append' or 'prepend', then any echo or print statements are captured and are either appended or prepended to the Response returned from the route callable. (Default: 'append')
  • determineRouteBeforeAppMiddleware
  • When true, the route is calculated before any middleware is executed. This means that you can inspect route parameters in middleware if you need to. (Default: false)
  • displayErrorDetails
  • When true, additional information about exceptions are displayed by the default error handler. (Default: false)
  • addContentLengthHeader
  • When true, Slim will add a Content-Length header to the response. If you are using a runtime analytics tool, such as New Relic, then this should be disabled. (Default: true)
  • routerCacheFile
  • Filename for caching the FastRoute routes. Must be set to to a valid filename within a writeable directory. If the file does not exist, then it is created with the correct cache information on first run. Set to false to disable the FastRoute cache system. (Default: false)