Php 域命名空间中的Laravel依赖项注入
我在使用依赖项注入和依赖项反转时遇到问题。我可以在构造函数中调用App::make,依赖项反转工作得很好。。。只有当我尝试将它注入构造函数时,我才会遇到问题Php 域命名空间中的Laravel依赖项注入,php,laravel-4,ioc-container,Php,Laravel 4,Ioc Container,我在使用依赖项注入和依赖项反转时遇到问题。我可以在构造函数中调用App::make,依赖项反转工作得很好。。。只有当我尝试将它注入构造函数时,我才会遇到问题 ReflectionException Class StationsInterface does not exist 一个符合这个要求的uri应该是…/stats/station/mogreet/6 文件结构: -app -controllers *StatsController -Dashboard
ReflectionException Class StationsInterface does not exist
一个符合这个要求的uri应该是…/stats/station/mogreet/6
文件结构:
-app
-controllers
*StatsController
-Dashboard
-Datasync
-Interfaces
*DatasyncInterface
*MogreetDatasyncInterface
-Services
*MogreetDatasync
*BackendServiceProvider
*DatasyncBase
-Repositories
-Interfaces
*CredentialsInterface
*StationsInterface
-Stations
*DbCredentials
*DbStations
*FileCredentials
-Stats
*BackendServiceProvider
*DbRepositoryBase
相关代码块如下所示:
服务提供者:
<?php namespace Dashboard\Repositories;
use Illuminate\Support\ServiceProvider;
class BackendServiceProvider extends ServiceProvider {
public function register() {
// Service Providers located in Stats directory
$this->app->bind('StatsDailyInterface', 'Dashboard\Repositories\Stats\DbStatsDaily');
//$this->app->bind('StatsMonthlyRepository', 'Dashboard\Repositories\Stats\DbStatsMonthly');
//$this->app->bind('StatsYearlyRepository', 'Dashboard\Repositories\Stats\DbStatsYearly');
// Service Providers located in Stations directory
$this->app->bind('CredentialsInterface', 'Dashboard\Repositories\Stations\FileCredentials');
$this->app->bind('StationsInterface', 'Dashboard\Repositories\Stations\DbStations');
}
}
这个问题的答案是ServiceDataSyncInterface的闭包。之前,我是这样定义绑定的:
$this->app->bind('MogreetDatasyncInterface', 'Dashboard\Datasync\Services\MogreetDatasync');
<?php namespace Dashboard\Datasync;
use Dashboard\Repositories\Interfaces\StationsInterface;
use Dashboard\Repositories\Interfaces\CredentialsInterface;
class DatasyncBase {
protected $Station;
protected $Credentials;
protected $results;
protected $stats;
public function __construct(StationsInterface $Station, CredentialsInterface $Credentials) {
$this->Station = $Station;
$this->Credentials = $Credentials;
$this->stats = array();
}
public function __destruct() {
unset($this->results);
unset($this->stats);
}
public function init() {}
protected function fetch($uri, $post_fields = '') {
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $uri);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($cURL, CURLOPT_POSTFIELDS, $post_fields);
$this->results = curl_exec($cURL);
curl_close($cURL);
}
}
但是,这不允许IoC“递归”注入反转依赖项,您必须使用App::make('InversionInterface')让IoC真正正确地解决这个问题
<?php namespace Dashboard\Datasync;
use Illuminate\Support\ServiceProvider;
use Dashboard\Datasync\Services\BrightCoveDatasync;
use Dashboard\Datasync\Services\FacebookDatasync;
use Dashboard\Datasync\Services\GoogleAnalyticsDatasync;
use Dashboard\Datasync\Services\LiquidCompassDatasync;
use Dashboard\Datasync\Services\MogreetDatasync;
use Dashboard\Datasync\Services\TwitterDatasync;
class BackendServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('BrightCoveDatasyncInterface', function() { return new BrightCoveDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('FacebookDatasyncInterface', function() { return new FacebookDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('GoogleAnalyticsDatasyncInterface', function() { return new GoogleAnalyticsDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('LiquidCompassDatasyncInterface', function() { return new LiquidCompassDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('MogreetDatasyncInterface', function() { return new MogreetDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('TwitterDatasyncInterface', function() { return new TwitterDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
}
}
在哪里实例化DatasyncBase类?如果希望laravel从IOC容器中注入依赖项,您是否意识到必须使用App::make()?DatasyncBase类只是Datasync的父类,因此它是通过继承MogreetDatasync实例化的。我没有意识到必须使用App::make()来注入依赖项,我的大多数示例都只使用bind。我将开始研究这种方法,但是如果你有一个好的例子/教程可以给我指点,那将非常感谢。从类型提示自动注入是Laravel所做的,而不是PHP所做的。可以从controllers构造函数中注入的原因是,它是用Laravel的IOC容器的build()方法实例化的。看看这门课,你会对所发生的事情有更好的了解。我甚至相信Laracasts有一个关于它的屏幕广播:)我正在尝试使用依赖反转和依赖注入。需要使用App::make-makes使IOC容器能够解析绑定。我仍然无法让依赖项反转工作。我将更新这个问题,以反映所做的代码更改以及目录结构。
$this->app->bind('MogreetDatasyncInterface', 'Dashboard\Datasync\Services\MogreetDatasync');
<?php namespace Dashboard\Datasync;
use Illuminate\Support\ServiceProvider;
use Dashboard\Datasync\Services\BrightCoveDatasync;
use Dashboard\Datasync\Services\FacebookDatasync;
use Dashboard\Datasync\Services\GoogleAnalyticsDatasync;
use Dashboard\Datasync\Services\LiquidCompassDatasync;
use Dashboard\Datasync\Services\MogreetDatasync;
use Dashboard\Datasync\Services\TwitterDatasync;
class BackendServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('BrightCoveDatasyncInterface', function() { return new BrightCoveDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('FacebookDatasyncInterface', function() { return new FacebookDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('GoogleAnalyticsDatasyncInterface', function() { return new GoogleAnalyticsDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('LiquidCompassDatasyncInterface', function() { return new LiquidCompassDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('MogreetDatasyncInterface', function() { return new MogreetDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
$this->app->bind('TwitterDatasyncInterface', function() { return new TwitterDatasync( $this->app->make('StationsInterface'), $this->app->make('CredentialsInterface') ); });
}
}
<?php namespace Dashboard\Datasync;
use Dashboard\Repositories\Interfaces\StationsInterface;
use Dashboard\Repositories\Interfaces\CredentialsInterface;
class DatasyncBase {
protected $Station;
protected $Credentials;
protected $results;
protected $stats;
public function __construct(StationsInterface $Station, CredentialsInterface $Credentials) {
$this->Station = $Station;
$this->Credentials = $Credentials;
$this->stats = array();
}
public function __destruct() {
unset($this->results);
unset($this->stats);
}
public function init() {}
protected function fetch($uri, $post_fields = '') {
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $uri);
curl_setopt($cURL, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($cURL, CURLOPT_POSTFIELDS, $post_fields);
$this->results = curl_exec($cURL);
curl_close($cURL);
}
}