php 无法解析的依赖解析 [Parameter #0 [ <required> $name ]]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24124360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unresolvable dependency resolving [Parameter #0 [ <required> $name ]]
提问by Martin
Warning:This question is Laravel 4 specific.
警告:这个问题是 Laravel 4 特有的。
I've been using Facades in my controllers before. Therefore I know the code is working. Now I need to introduce dependency injection for various reasons.
我之前一直在我的控制器中使用 Facades。因此我知道代码正在运行。现在由于各种原因需要引入依赖注入。
After refactoring the controller I get following error:
重构控制器后,我收到以下错误:
Illuminate \ Container \ BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ $name ]].
照亮 \ 容器 \ BindingResolutionException
无法解析的依赖解析 [Parameter #0 [ $name ]]。
I can't figure out where the problem is. The Error message seems cryptic to me and I don't understand it. (I don't see any problem with my __constructor
parameters since I've registered the binding for the HelpersInterface
)
我无法弄清楚问题出在哪里。错误消息对我来说似乎很神秘,我不明白。(我没有看到我的__constructor
参数有任何问题,因为我已经为 注册了绑定HelpersInterface
)
Here are the important parts of my code:
以下是我的代码的重要部分:
File: app/start/global.php
文件:app/start/global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers');
File: composer.json
文件:composer.json
// ...
"autoload": {
// ...
"psr-0": {
"Acme": "app/"
}
},
// ...
File: app/Acme/Controllers/BaseController.php
文件: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();
}
}
File: app/Acme/Services/Helpers.php
文件: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
) {
// ...
}
// ...
}
File: app/Acme/Providers/HelpersServiceProvider.php
文件: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']
);
});
}
回答by Joseph Silber
It seems your Acme\Services\Helpers
constructor takes a $name
parameter, but is not type hinted.
似乎您的Acme\Services\Helpers
构造函数接受一个$name
参数,但没有类型提示。
Laravel's IoC is not magic. If your don't provide a type hint for every parameter, the IoC container has no way of knowing what to pass in.
Laravel 的 IoC 并不神奇。如果您没有为每个参数提供类型提示,则 IoC 容器无法知道要传入什么。
回答by CIRCLE
For me it was just a matter of running
对我来说这只是跑步的问题
php artisan optimize:clear
回答by Martin
Got it fixed. All the tutorials about dependency injection were referring to concrete implementations of interfaces so that I thought that's the way to go about it. Joseph Silber's
answer got me on the right track.
搞定了 所有关于依赖注入的教程都指的是接口的具体实现,所以我认为这是解决问题的方法。Joseph Silber's
答案让我走上正轨。
The trick is to bind the Interface to the binding of the ServiceProvider
like shown below. That way Laravel will know how to instantiate the Helpers service.
诀窍是将接口绑定到ServiceProvider
如下所示的绑定。这样 Laravel 就会知道如何实例化 Helpers 服务。
File: app/start/global.php
文件:app/start/global.php
<?php
// ...
App::bind('Acme\Interfaces\HelpersInterface', 'helpers');