davidepastore / slim-config
A slim middleware to read configuration from different files based on hassankhan/config
Requires
- php: ^7.2 || ^8.0
- hassankhan/config: ^0.10.0
Requires (Dev)
- phpunit/phpunit: ^8.5.52 || ^9.6.33
- scrutinizer/ocular: ~1.1
- slim/slim: ~3.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-25 13:51:14 UTC
README
A file configuration loader that supports PHP, INI, XML, JSON, and YML files for the Slim Framework. It internally uses hassankhan/config.
Install
Via Composer
$ composer require davidepastore/slim-config
Requires PHP 7.2 or newer (including PHP 8.x) and Slim 3.0.0 or newer.
Usage
In most cases you want to register DavidePastore\Slim\Config for a single route, however,
as it is middleware, you can also register it for all routes.
Register per route
$app = new \Slim\App(); // Fetch DI Container $container = $app->getContainer(); // Register provider $container['config'] = function () { //Create the configuration return new \DavidePastore\Slim\Config\Config('config.json'); }; $app->get('/api/myEndPoint',function ($req, $res, $args) { //Here you have your configuration $config = $this->config->getConfig(); $secret = $config->get('security.secret'); })->add($container->get('config')); $app->run();
Register for all routes
$app = new \Slim\App(); // Fetch DI Container $container = $app->getContainer(); // Register provider $container['config'] = function () { //Create the configuration return new \DavidePastore\Slim\Config\Config('config.json'); }; // Register middleware for all routes // If you are implementing per-route checks you must not add this $app->add($container->get('config')); $app->get('/foo', function ($req, $res, $args) { //Here you have your configuration $config = $this->config->getConfig(); $secret = $config->get('security.secret'); }); $app->post('/bar', function ($req, $res, $args) { //Here you have your configuration $config = $this->config->getConfig(); $ttl = $config->get('app.timeout', 3000); }); $app->run();
Using the Slim Skeleton
The examples above keep everything in one file. If your project is based on the Slim Skeleton (3.x), the dependencies, middleware, and routes live in separate files under src/. Split the code across them like this:
src/dependencies.php: register the provider:
<?php use Slim\App; return function (App $app) { $container = $app->getContainer(); // ... // Register provider $container['config'] = function ($c) { //Create the configuration return new \DavidePastore\Slim\Config\Config(__DIR__ . '/../config.json'); }; };
src/middleware.php: register the middleware for all routes (skip this if you register it per route):
<?php use Slim\App; return function (App $app) { $app->add($app->getContainer()->get('config')); };
src/routes.php: read the configuration in your routes, and add the middleware to individual routes if you register it per route:
<?php use Slim\App; use Slim\Http\Request; use Slim\Http\Response; return function (App $app) { $container = $app->getContainer(); // Middleware registered for all routes in src/middleware.php $app->get('/foo', function (Request $request, Response $response, array $args) { //Here you have your configuration $config = $this->config->getConfig(); $secret = $config->get('security.secret'); }); // Middleware registered only for this route $app->get('/api/myEndPoint', function (Request $request, Response $response, array $args) { //Here you have your configuration $config = $this->config->getConfig(); $secret = $config->get('security.secret'); })->add($container->get('config')); };
In older 3.x versions of the skeleton these files are plain scripts instead of closures. In that case, put the code from inside each closure directly in the matching file.
Where are the benefits?
The configuration is loaded from the filesystem only when the given route is called in the per route usage. In the other case (all routes) the config should be general and used in the whole routes, because it's read in every request.
Just the tip of the iceberg!
You can read the hassankhan/config documentation here for more info.
Testing
$ phpunit
Contributing
Please see CONTRIBUTING for details.