属性 Slim依赖容器的使用方法
Propery way to use Slim's dependency container
按照http://www.slimframework.com/docs/tutorial/first-app.html,先创建slim对象,然后获取容器,添加服务
$app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();
$container['logger'] = function($c) {
...
return $logger;
};
但是,专门针对依赖容器的 http://www.slimframework.com/docs/concepts/di.html 更强大,并指出:
You don’t have to provide a dependency container. If you do, however,
you must inject the container instance into the Slim application’s
constructor.
$container = new \Slim\Container;
$app = new \Slim\App($container);
一种方式比另一种更合适吗?
使用第二种方法时如何添加服务?
Is one way more proper than the other?
几乎是一样的,所以在我看来没有合适的方法,但我正在做第二种方法,因为这样你可以在创建实际的苗条应用程序实例之前添加记录器和其他东西。
How are services added when using the second approach?
与使用您的第一种方法相同
$container = new \Slim\Container;
$container['logger'] = function($c) {
...
return $logger;
};
$app = new \Slim\App($container);
按照http://www.slimframework.com/docs/tutorial/first-app.html,先创建slim对象,然后获取容器,添加服务
$app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();
$container['logger'] = function($c) {
...
return $logger;
};
但是,专门针对依赖容器的 http://www.slimframework.com/docs/concepts/di.html 更强大,并指出:
You don’t have to provide a dependency container. If you do, however, you must inject the container instance into the Slim application’s constructor.
$container = new \Slim\Container; $app = new \Slim\App($container);
一种方式比另一种更合适吗?
使用第二种方法时如何添加服务?
Is one way more proper than the other?
几乎是一样的,所以在我看来没有合适的方法,但我正在做第二种方法,因为这样你可以在创建实际的苗条应用程序实例之前添加记录器和其他东西。
How are services added when using the second approach?
与使用您的第一种方法相同
$container = new \Slim\Container;
$container['logger'] = function($c) {
...
return $logger;
};
$app = new \Slim\App($container);