无法解析的依赖项解析[参数#0 [<required> $ name]]

Mar*_*tin 18 php oop dependency-injection laravel laravel-4

警告:这个问题是Laravel 4的具体问题.

我之前一直在我的控制器中使用Facades.因此我知道代码正在运行.现在我需要出于各种原因引入依赖注入.

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

Illuminate\Container\BindingResolutionException

无法解析的依赖项解析[参数#0 [$ name]].

我无法弄清楚问题出在哪里.错误消息对我来说似乎很神秘,我不明白.(我没有看到我的__constructor参数有任何问题,因为我已经注册了绑定HelpersInterface)

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

文件:app/start/global.php

<?php

// ...

App::bind('Acme\Interfaces\HelpersInterface', 'Acme\Services\Helpers');
Run Code Online (Sandbox Code Playgroud)

文件:composer.json

// ...

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

// ...
Run Code Online (Sandbox Code Playgroud)

文件: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();
    }

}
Run Code Online (Sandbox Code Playgroud)

文件: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
    ) {
        // ...
    }

// ...

}
Run Code Online (Sandbox Code Playgroud)

文件: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']
        );
    });
}
Run Code Online (Sandbox Code Playgroud)

CIR*_*CLE 26

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

php artisan optimize:clear
Run Code Online (Sandbox Code Playgroud)

  • 也解决了我的问题。 (2认同)

Jos*_*ber 20

看起来您的Acme\Services\Helpers构造函数接受一个$name参数,但不是类型提示.

Laravel的IoC并不神奇.如果您没有为每个参数提供类型提示,则IoC容器无法知道要传递的内容.


小智 8

确保使用 Illuminate\Http\Request;在文件顶部,而不是像这样的任何其他 http 导入

use Illuminate\Http\Request; 
Run Code Online (Sandbox Code Playgroud)

晚点再谢我!