問題描述
警告:這個問題是針對 Laravel 4 的.
Warning: This question is Laravel 4 specific.
我之前一直在我的控制器中使用 Facades.因此我知道代碼正在運行.現在由于各種原因需要引入依賴注入.
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.
重構控制器后出現以下錯誤:
After refactoring the controller I get following error:
照亮容器BindingResolutionException
Illuminate Container BindingResolutionException
無法解析的依賴解析 [Parameter #0 [ $name ]].
Unresolvable dependency resolving [Parameter #0 [ $name ]].
我不知道問題出在哪里.錯誤消息對我來說似乎很神秘,我不明白.(我沒有發現 __constructor
參數有任何問題,因為我已經為 HelpersInterface
注冊了綁定)
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
)
以下是我的代碼的重要部分:
Here are the important parts of my code:
文件:app/start/global.php
<?php
// ...
App::bind('AcmeInterfacesHelpersInterface', 'AcmeServicesHelpers');
文件:composer.json
// ...
"autoload": {
// ...
"psr-0": {
"Acme": "app/"
}
},
// ...
文件:app/Acme/Controllers/BaseController.php
<?php namespace AcmeControllers;
use CarbonCarbon;
use Controller;
use IlluminateFoundationApplication as App;
use IlluminateViewFactory as View;
use AcmeInterfacesHelpersInterface as Helpers;
use IlluminateHttpResponse;
class BaseController extends Controller {
/**
* @var IlluminateFoundationApplication
*/
private $app;
/**
* @var CarbonCarbon
*/
private $carbon;
/**
* @var IlluminateViewFactory
*/
private $view;
/**
* @var AcmeInterfacesHelpersInterface
*/
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 AcmeServices;
use IlluminateConfigRepository as Config;
use IlluminateDatabaseConnection as DB;
use IlluminateHttpRequest;
use IlluminateRoutingRedirector as Redirect;
use IlluminateSessionStore as Session;
use IlluminateSupportFacadesResponse;
use IlluminateTranslationTranslator as Lang;
use IlluminateViewFactory as View;
use AcmeInterfacesMockablyInterface;
use MonologLogger 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 AcmeProviders;
use IlluminateSupportServiceProvider;
use AcmeServicesHelpers;
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['IlluminateSupportFacadesResponse']
);
});
}
推薦答案
看起來你的 AcmeServicesHelpers
構造函數接受了一個 $name
參數,但不是類型暗示.
It seems your AcmeServicesHelpers
constructor takes a $name
parameter, but is not type hinted.
Laravel 的 IoC 并不神奇.如果您沒有為每個參數提供類型提示,IoC 容器將無法知道要傳入什么.
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.
這篇關于無法解析的依賴解析 [Parameter #0 [ <required>$姓名]]的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!