Git Product home page Git Product logo

slim-config's Introduction

Slim Framework Config

Latest version Build Status Coverage Status Quality Score Total Downloads PSR2 Conformance

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 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();

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.

Credits

slim-config's People

Contributors

davidepastore avatar jbulava avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

slim-config's Issues

Ability to access configuration on middleware

This is more of a question than anything. As I read on the documentation there are two possible ways to load the configuration values:

  • Per route
  • System-wide

However, how can I load the configuration within a callable for a middleware? take the following code:

$app->add(function($request, $response, $next) {
    $this->db = new DatabaseManager('host', 'user', 'pwd', 'database');
    $response = $next($request, $response);
    return $response;
});

In the example above I'm adding the db instance so I can do queries within my routes, however, the configuration instance is not available at that point, so if I do this:

$app->add(function($request, $response, $next) {
    $config = $this->config->getConfig();
    $host = $config->get('database.host');
    $this->db = new DatabaseManager($host, 'user', 'pwd', 'database');
    $response = $next($request, $response);
    return $response;
});

It won't work because $config is null (or the return value of getConfig() for that matter). Is there a way to make that work? Maybe I'm missing something or I still don't completely understand the order of execution in the middleware/container.

Thanks in advance!

Is YML also allowed?

Hi,

just a question. In the README.md you set the things up with a config.json. Can this also be a YML file or has it to be a JSON? If so, would it be easy to add YML as supported config file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.