Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slim framework configuration outside of Slim

My Slim project is organised as follows:

- app
-- Acme
--- Auth
---- Auth.php (handles authentication)
-- config
--- development.php
--- production.php
-- routes
-- views
- public
- vendor

I'm setting up my app in the usual way.

$app = new \Slim\Slim([
    'view' => new \Slim\Views\Twig(),
    'mode' => 'development'
]);

And injecting dependancies like this.

$app->auth = function($app) {
    return new Codecourse\Auth\Auth($app->user);
};

What's the most correct way to allow my Auth class to see my configuration? I was originally going to pass it in as a dependancy, but Slim's configuration keys are accessed like $app->config('key') so I'd have to pass in $app, which would be bad.

I'm aware that my authentication could be served as middleware, but would like to have access to configuration globally.

Would it perhaps be better to use a package like noodlehaus/config (https://github.com/noodlehaus/config) to handle configuration outside of Slim?

like image 857
Alex Garrett Avatar asked Nov 01 '22 11:11

Alex Garrett


1 Answers

After you instantiated Slim\Slim you can access its instances through the static method Slim\Slim::getInstance() from anywhere (e.g. inside your Auth class) and then access any of its config properties with the config('key') method (i.e. you can use Slim as a resource locator to get/set really any of the active instance's resources from anywhere). And this way there is no need for passing around the application object.

But of course you can always have a separate config object (like the one from the noodlehaus/config package) and use that instead of Slim's built-in resource locator feature ... this way you can access it without instantiating any Slim application objects and have the Auth library be independent from the Slim framework.

like image 74
Martin Turjak Avatar answered Nov 15 '22 06:11

Martin Turjak