小编Fre*_*ead的帖子

如何在Laravel之外使用Laravel的IOC容器进行方法注入

简短的说明:我无法使用composer(https://packagist.org/packages/illuminate/container)安装Laravel容器进行方法注入.仅当在对象的构造函数中使用时,注入才有效.例如:

class SomeClass {
    function __construct(InjectedClassWorksHere $obj) {}
    function someFunction(InjectedClassFailsHere $obj) {}
}
Run Code Online (Sandbox Code Playgroud)

长话故事:我正在考虑重新考虑使用Laravel的一个重大项目,但由于业务压力,我无法投入我想要的时间.为了不让"宝宝带着洗澡水",我正在使用各个Laravel组件来提升旧分支中开发的代码的优雅.在评估Laravel时我最喜欢的一种新技术是依赖注入的概念.我很高兴后来发现我可以使用Laravel项目的外部.我现在有这个工作,一切都很好,除了在线发现的容器的开发版本似乎不支持方法注入.

有没有其他人能够让容器工作并在Laravel项目之外进行方法注入?

我的方法到目前为止......

composer.json

"illuminate/support": "5.0.*@dev",
"illuminate/container": "5.0.*@dev",
Run Code Online (Sandbox Code Playgroud)

应用程序引导代码:

use Illuminate\Container\Container;

$container = new Container();
$container->bind('app', self::$container); //not sure if this is necessary

$dispatcher = $container->make('MyCustomDispatcher');
$dispatcher->call('some URL params to find controller');
Run Code Online (Sandbox Code Playgroud)

有了上面的内容,我可以注入我的控制器的构造函数,但不能注入它们的方法方法.我错过了什么?

完整源代码...(C:\ workspace\LMS> php cmd\test_container.php)

<?php

// This sets up my include path and calls the composer autoloader
require_once "bare_init.php";

use Illuminate\Container\Container;
use Illuminate\Support\ClassLoader;
use Illuminate\Support\Facades\Facade;

// Get a reference to the root …
Run Code Online (Sandbox Code Playgroud)

php dependency-injection illuminate-container laravel-5

4
推荐指数
1
解决办法
4974
查看次数