Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

php - Unresolvable dependency resolving [Parameter #0 [ <required> $name ]]

Warning: This question is Laravel 4 specific.

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:

Illuminate Container BindingResolutionException

Unresolvable dependency resolving [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)

Here are the important parts of my code:

File: app/start/global.php

<?php
 
// ...
 
App::bind('AcmeInterfacesHelpersInterface', 'AcmeServicesHelpers');

File: composer.json

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

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

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

// ...

}

File: 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']
        );
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For me it was just a matter of running

php artisan optimize:clear

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...