Slim 3 + Pleague 2.4 - Alias(路由器)不受容器管理?

时间:2017-09-09 11:55:51

标签: ioc-container slim-3 thephpleague

如何将Pleague v2.4与Slim v3.8.1一起使用?

example之后,我尝试了:

// PSR 7 standard.
use Slim\Http\Request;
use Slim\Http\Response;

// Import classes.
use Slim\App as Slim;

use League\Container\Container;
$container = new Container;

// Required to enable auto wiring.
$container->delegate(
    new \League\Container\ReflectionContainer
);

// Get an instance of Slim.
$app = new Slim($container);

我明白了:

  

致命错误:Uncaught League \ Container \ Exception \ NotFoundException:   别名(路由器)不受容器管理   /var/www/html/projectA/vendor/league/container/src/Container.php:266   堆栈跟踪:#0   /var/www/html/projectA/vendor/league/container/src/Container.php(93):   League \ Container \ Container-> getFromDelegate('router',Array)#1   /var/www/html/projectA/vendor/slim/slim/Slim/App.php(239):   League \ Container \ Container-> get('router')#2   /var/www/html/projectA/vendor/slim/slim/Slim/App.php(143):   Slim \ App-> map(Array,'/',Object(Closure))#3   /var/www/html/projectA/public/index.php(53):Slim \ App-> get('/',   Object(Closure))#4 {main}引入   /var/www/html/projectA/vendor/league/container/src/Container.php on   第266行

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Slim正在尝试访问您添加的容器上未定义的router。在Slim中,它们将由\Slim\DefaultServicesProvider添加。 但是你不能使用它,因为你的DI容器不支持数组访问。

修复此问题的一个选项是将Slim容器设置为委托,然后League\Container\Container将通过slim容器获取路由器和所有其他内容:

$container = new \League\Container\Container;
$container->delegate(new \Slim\Container());
$app = new \Slim\App($container);