问题描述
警告:此问题是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:
不可解析的依赖关系解析[参数#0 [$ name]]。
Unresolvable dependency resolving [Parameter #0 [ $name ]].
我不知道问题在哪里。错误消息对我来说似乎很神秘,我不明白。 (我没有看到我的 __构造函数
参数的任何问题,因为我注册了 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('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']
);
});
}
推荐答案
$ c> Acme\Services\Helpers 构造函数采用 $ name
参数,但不是类型暗示。
It seems your Acme\Services\Helpers
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.
这篇关于无法解析的依赖关系[参数#0 [< required> $ name]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!