简体   繁体   English

使用Slim的依赖容器的合适方式

[英]Propery way to use Slim's dependency container

According to http://www.slimframework.com/docs/tutorial/first-app.html , the slim object is first created, and then the container is gotten and services are added to it. 根据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;
};

However, http://www.slimframework.com/docs/concepts/di.html which is specifically about the dependency container is much stronger and states: 但是,具体关于依赖容器的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. 但是,如果这样做,则必须将容器实例注入Slim应用程序的构造函数中。

$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? 使用第二种方法时如何添加服务?

Is one way more proper than the other? 一种方式比另一种更合适吗?

There are nealy the same, so in my opinion there is no proper way, but I've doing the second approach, because this way you can add logger and other stuff before you create the actual slim app instance. 有一些相同,所以在我看来没有正确的方法,但我正在做第二种方法,因为这样你可以在创建实际的苗条应用程序实例之前添加记录器和其他东西。

How are services added when using the second approach? 使用第二种方法时如何添加服务?

The same as using you'r first approach 与使用第一种方法相同

$container = new \Slim\Container;
$container['logger'] = function($c) {
    ...
    return $logger;
};

$app = new \Slim\App($container);

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

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