【发布时间】:2015-10-06 07:24:02
【问题描述】:
我有两个 index.php 并且都使用了一个 bootstrap.php。引导文件正在设置一个 DI 容器,我需要在两个索引文件中访问这个 DI 容器。
首先我想在 bootstrap.php 中使用一个简单的return:
bootstrap.php
<?php
require __DIR__ . '/vendor/autoload.php';
$container = new League\Container\Container;
// add some services
return $container;
index.php
<?php
$container = require __DIR__ . '/bootstrap.php';
$container->get('application')->run();
我在某处读到,使用这样的 return 语句是一个坏习惯。所以我想知道如何使 index.php 中的容器以一种简单而正确的方式访问?
【问题讨论】:
-
如果你真的需要 >>samestackoverflow.com/a/203359/5297359
-
一个 index.php 是前端的起点,另一个是我的应用程序后端的起点。这两个索引文件不在同一个请求中执行 - 所以不,我不需要相同的容器对象。我只是想知道如何访问容器实例。我不想创建单例或注册表,因为我只会将它们用于这个单一目的。
-
OK 不只是将返回值放在
bootstrap.php中,只需使用简单的require而不将其分配给$container,然后您可以使用引导程序中的$container变量而无需执行任何其他操作