警告:此问题是特定于Laravel 4的。
之前我一直在我的控制器中使用Facades。因此我知道代码正在运行。现在我需要出于各种原因引入依赖注入。
重构控制器后,出现以下错误:
Illuminate \ Container \ BindingResolutionException
无法解析的依赖项解析[参数#0 [$ name]]。
我无法找出问题所在。错误消息对我来说似乎很神秘,我不理解它。 (因为我已经注册了__constructor
的绑定,所以我的HelpersInterface
参数没有任何问题
以下是我的代码的重要部分:
文件:app / start / global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers');
文件:composer.json
// ...
"autoload": {
// ...
"psr-0": {
"Acme": "app/"
}
},
// ...
文件:app / Acme / Controllers / BaseController.php
<?php namespace Acme\Controllers;
use Carbon\Carbon;
use Controller;
use Illuminate\Foundation\Application as App;
use Illuminate\View\Factory as View;
use Acme\Interfaces\HelpersInterface as Helpers;
use Illuminate\Http\Response;
class BaseController extends Controller {
/**
* @var \Illuminate\Foundation\Application
*/
private $app;
/**
* @var \Carbon\Carbon
*/
private $carbon;
/**
* @var \Illuminate\View\Factory
*/
private $view;
/**
* @var \Acme\Interfaces\HelpersInterface
*/
private $helpers;
function __construct(App $app, Carbon $carbon, View $view, Helpers $helpers)
{
$this->app = $app;
$this->carbon = $carbon;
$this->view = $view;
$this->helpers = $helpers;
$lang = $this->app->getLocale();
$now = $this->carbon->now();
$this->view->share('lang', $lang);
$this->view->share('now', $now);
}
/**
* Missing Method
*
* Abort the app and return a 404 response
*
* @param array $parameters
* @return Response
*/
public function missingMethod($parameters = array())
{
return $this->helpers->force404();
}
}
文件:app / Acme / Services / Helpers.php
<?php namespace Acme\Services;
use Illuminate\Config\Repository as Config;
use Illuminate\Database\Connection as DB;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector as Redirect;
use Illuminate\Session\Store as Session;
use Illuminate\Support\Facades\Response;
use Illuminate\Translation\Translator as Lang;
use Illuminate\View\Factory as View;
use Acme\Interfaces\MockablyInterface;
use Monolog\Logger as Log;
class Helpers implements HelpersInterface {
// ...
public function __construct(
Config $config,
Lang $lang,
View $view,
MockablyInterface $mockably,
Log $log,
Request $request,
Session $session,
DB $db,
Redirect $redirect,
Response $response
) {
// ...
}
// ...
}
文件:app / Acme / Providers / HelpersServiceProvider.php
<?php namespace Acme\Providers;
use Illuminate\Support\ServiceProvider;
use Acme\Services\Helpers;
class HelpersServiceProvider extends ServiceProvider {
private $db;
private $defaultDbConnection;
protected function init()
{
$this->db = $this->app['db'];
$this->defaultDbConnection = $this->db->getDefaultConnection();
}
public function register()
{
$this->init();
$this->app->bind('helpers', function ()
{
return new Helpers(
$this->app['config'],
$this->app['translator'],
$this->app['view'],
$this->app['mockably'],
$this->app->make('log')->getMonolog(),
$this->app['request'],
$this->app['session.store'],
$this->db->connection($this->defaultDbConnection),
$this->app['redirect'],
$this->app['Illuminate\Support\Facades\Response']
);
});
}
答案 0 :(得分:17)
您的Acme\Services\Helpers
构造函数似乎采用$name
参数,但未提示类型。
Laravel的IoC并不神奇。如果您没有为每个参数提供类型提示,则IoC容器无法知道要传递的内容。
答案 1 :(得分:2)
修好了。关于依赖注入的所有教程都是指接口的具体实现,因此我认为这是实现它的方法。 Joseph Silber's
回答让我走上正轨。
诀窍是将接口绑定到ServiceProvider
的绑定,如下所示。这样Laravel就会知道如何实例化Helpers服务。
文件:app / start / global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'helpers');
答案 2 :(得分:2)
对我来说,这只是跑步的问题
php artisan optimize:clear