简体   繁体   English

Slim之外的Slim框架配置

[英]Slim framework configuration outside of Slim

My Slim project is organised as follows: 我的Slim项目组织如下:

- 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? 允许我的Auth类查看我的配置的最正确方法是什么? 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. 我本来是作为依赖来传递它的,但是Slim的配置键的访问方式类似于$app->config('key')因此我必须传递$app ,这很不好。

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? 使用诸如oodlenhaus / config( https://github.com/noodlehaus/config )之类的软件包来处理Slim之外的配置是否会更好?

After you instantiated Slim\\Slim you can access its instances through the static method Slim\\Slim::getInstance() from anywhere (eg inside your Auth class) and then access any of its config properties with the config('key') method (ie you can use Slim as a resource locator to get/set really any of the active instance's resources from anywhere). 实例化Slim\\Slim您可以从任何地方(例如,在Auth类内部Slim\\Slim::getInstance()通过静态方法Slim\\Slim::getInstance()访问其实例,然后使用config('key')方法访问其任何配置属性(也就是说,您可以将Slim用作资源定位器,以从任何地方获取/设置任何活动实例的资源 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. 但是,当然,您始终可以拥有一个单独的配置对象(例如,noodlehaus / config包中的配置对象),并使用该对象代替Slim的内置资源定位器功能...这样,您无需实例化任何Slim应用程序对象就可以访问它,并且使Auth库独立于Slim框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM