无法解决的依赖关系解析 [参数 #0 [ <必需> $name ]]

警告:这个问题是Laravel 4特定的。

我以前一直在控制器中使用Facades。因此,我知道代码正在工作。现在我需要引入依赖注入,原因有很多。

重构控制器后,我收到以下错误:

Illuminate \ Container \ BindingSolutionException

无法解决的依赖关系解析 [参数 #0 [ $name ]]。

我不知道问题出在哪里。错误消息对我来说似乎很神秘,我不明白。(我没有看到我的参数有任何问题,因为我已经注册了__constructorHelpersInterface)

以下是我的代码的重要部分:

文件:应用/开始/全局.php

<?php
 
// ...
 
App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers');

File: composer.json

// ...
 
"autoload": {
    // ...
    "psr-0": {
        "Acme": "app/"
    }
},
 
// ...

文件:应用程序/Acme/控制器/基本控制器.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();
    }
 
}

文件:应用程序/Acme/服务/助手.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/HelpersService Provider.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']
        );
    });
}

答案 1

对我来说,这只是一个跑步的问题

php artisan optimize:clear

答案 2

您的构造函数似乎采用了一个参数,但没有类型提示。Acme\Services\Helpers$name

Laravel的IoC不是魔术。如果不为每个参数提供类型提示,则 IoC 容器无法知道要传入的内容。


推荐