【发布时间】:2014-11-08 09:11:47
【问题描述】:
小故事: 我无法使用 Composer (https://packagist.org/packages/illuminate/container) 安装的 Laravel 容器进行方法注入。注入只有在对象的构造函数中使用时才有效。例如:
class SomeClass {
function __construct(InjectedClassWorksHere $obj) {}
function someFunction(InjectedClassFailsHere $obj) {}
}
长篇大论: 我正在考虑将一个大型项目重构为使用 Laravel,但由于业务压力,我无法投入我想要的时间。为了不把“婴儿和洗澡水一起扔出去”,我使用了单独的 Laravel 组件来提高在旧分支中开发的代码的优雅性。在评估 Laravel 时,我最喜欢的新技术之一是依赖注入的概念。后来我很高兴地发现我可以在 Laravel 项目之外使用它。我现在有这个工作,一切都很好,除了网上找到的容器的开发版本似乎不支持方法注入。
有没有其他人能够让容器工作并在 Laravel 项目之外进行方法注入?
到目前为止我的方法...
composer.json
"illuminate/support": "5.0.*@dev",
"illuminate/container": "5.0.*@dev",
应用程序引导代码:
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');
通过以上内容,我可以注入控制器的构造函数,但不能注入它们的方法方法。我错过了什么?
完整源代码... (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 of the includes directory
$basePath = dirname(dirname(__FILE__));
ClassLoader::register();
ClassLoader::addDirectories([
$basePath
]);
$container = new Container();
$container->bind('app', $container);
$container->bind('path.base', $basePath);
class One {
public $two;
public $say = 'hi';
function __construct(Two $two) {
$this->two = $two;
}
}
Class Two {
public $some = 'thing';
public function doStuff(One $one) {
return $one->say;
}
}
/* @var $one One */
$one = $container->make(One);
var_dump($one);
print $one->two->doStuff();
当我运行上述内容时,我得到...
C:\workspace\LMS>php cmd\test_container.php
object(One)#9 (2) {
["two"]=>
object(Two)#11 (1) {
["some"]=>
string(5) "thing"
}
["say"]=>
string(2) "hi"
}
PHP Catchable fatal error: Argument 1 passed to Two::doStuff() must be an instance of One, none
given, called in C:\workspace\LMS\cmd\test_container.php on line 41
and defined in C:\workspace\LMS\cmd\test_container.php on line 33
Catchable fatal error: Argument 1 passed to Two::doStuff() must be an instance of One, none
given, called in C:\workspace\LMS\cmd\test_container.php on line 41 and
defined in C:\workspace\LMS\cmd\test_container.php on line 33
或者,一个更基本的示例说明注入在构造函数中工作,而不是在方法中工作......
class One {
function __construct(Two $two) {}
public function doStuff(Three $three) {}
}
class Two {}
class Three {}
$one = $container->make(One); // totally fine. Injection works
$one->doStuff(); // Throws Exception. (sad trombone)
【问题讨论】:
-
是的,这个设置在构造函数和方法中都对我有用(尽管我确实需要在 composer.json 中包含
"illuminate/contracts": "5.0.*@dev")。您能否分享在SomeClass中调用SomeFunction的代码以及返回的内容? -
顺便说一下,Matt Stauffer 有一个很棒的演讲,讨论了您的增量方法,他称之为"Undercover Laravel"。您可能还会发现他的项目Illuminate Non-Laravel 很有趣,尽管他使用的是 v4.2.8,而不是 v5。
-
今晚我会看看那个演讲。看起来很有趣,当然也很相关!
-
将编辑我的帖子以添加我的示例的完整代码
标签: php dependency-injection illuminate-container laravel-5