Slim 之外的 Slim 框架配置
Slim framework configuration outside of Slim
我的 Slim 项目组织如下:
- app
-- Acme
--- Auth
---- Auth.php (handles authentication)
-- config
--- development.php
--- production.php
-- routes
-- views
- public
- vendor
我正在以常规方式设置我的应用程序。
$app = new \Slim\Slim([
'view' => new \Slim\Views\Twig(),
'mode' => 'development'
]);
并像这样注入依赖关系。
$app->auth = function($app) {
return new Codecourse\Auth\Auth($app->user);
};
允许我的 Auth class 查看我的配置的最正确方法是什么?我原本打算将它作为依赖项传递,但 Slim 的配置键像 $app->config('key')
一样访问,所以我必须传递 $app
,这很糟糕。
我知道我的身份验证可以用作中间件,但我想在全局范围内访问配置。
使用像 noodlehaus/config (https://github.com/noodlehaus/config) 这样的包来处理 Slim 之外的配置会更好吗?
实例化 Slim\Slim
后,您可以从任何地方通过静态方法 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 访问它的实例)。这样就不需要传递应用程序对象了。
但当然你总是可以有一个单独的配置对象(比如来自 noodlehaus/config 包的那个)并使用它来代替 Slim 的内置资源定位器功能......这样你就可以访问它无需实例化任何 Slim 应用程序对象,并让 Auth
库独立于 Slim 框架。
我的 Slim 项目组织如下:
- app
-- Acme
--- Auth
---- Auth.php (handles authentication)
-- config
--- development.php
--- production.php
-- routes
-- views
- public
- vendor
我正在以常规方式设置我的应用程序。
$app = new \Slim\Slim([
'view' => new \Slim\Views\Twig(),
'mode' => 'development'
]);
并像这样注入依赖关系。
$app->auth = function($app) {
return new Codecourse\Auth\Auth($app->user);
};
允许我的 Auth class 查看我的配置的最正确方法是什么?我原本打算将它作为依赖项传递,但 Slim 的配置键像 $app->config('key')
一样访问,所以我必须传递 $app
,这很糟糕。
我知道我的身份验证可以用作中间件,但我想在全局范围内访问配置。
使用像 noodlehaus/config (https://github.com/noodlehaus/config) 这样的包来处理 Slim 之外的配置会更好吗?
实例化 Slim\Slim
后,您可以从任何地方通过静态方法 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 访问它的实例)。这样就不需要传递应用程序对象了。
但当然你总是可以有一个单独的配置对象(比如来自 noodlehaus/config 包的那个)并使用它来代替 Slim 的内置资源定位器功能......这样你就可以访问它无需实例化任何 Slim 应用程序对象,并让 Auth
库独立于 Slim 框架。