<bdo id='3a4Aw'></bdo><ul id='3a4Aw'></ul>
    <i id='3a4Aw'><tr id='3a4Aw'><dt id='3a4Aw'><q id='3a4Aw'><span id='3a4Aw'><b id='3a4Aw'><form id='3a4Aw'><ins id='3a4Aw'></ins><ul id='3a4Aw'></ul><sub id='3a4Aw'></sub></form><legend id='3a4Aw'></legend><bdo id='3a4Aw'><pre id='3a4Aw'><center id='3a4Aw'></center></pre></bdo></b><th id='3a4Aw'></th></span></q></dt></tr></i><div id='3a4Aw'><tfoot id='3a4Aw'></tfoot><dl id='3a4Aw'><fieldset id='3a4Aw'></fieldset></dl></div>

  1. <legend id='3a4Aw'><style id='3a4Aw'><dir id='3a4Aw'><q id='3a4Aw'></q></dir></style></legend>

  2. <small id='3a4Aw'></small><noframes id='3a4Aw'>

  3. <tfoot id='3a4Aw'></tfoot>

      无法解析的依赖解析[参数#0 [&lt;required&gt;$名称]]

      时间:2023-11-01

      1. <small id='PO7mo'></small><noframes id='PO7mo'>

          <bdo id='PO7mo'></bdo><ul id='PO7mo'></ul>
            <i id='PO7mo'><tr id='PO7mo'><dt id='PO7mo'><q id='PO7mo'><span id='PO7mo'><b id='PO7mo'><form id='PO7mo'><ins id='PO7mo'></ins><ul id='PO7mo'></ul><sub id='PO7mo'></sub></form><legend id='PO7mo'></legend><bdo id='PO7mo'><pre id='PO7mo'><center id='PO7mo'></center></pre></bdo></b><th id='PO7mo'></th></span></q></dt></tr></i><div id='PO7mo'><tfoot id='PO7mo'></tfoot><dl id='PO7mo'><fieldset id='PO7mo'></fieldset></dl></div>
          • <legend id='PO7mo'><style id='PO7mo'><dir id='PO7mo'><q id='PO7mo'></q></dir></style></legend>
              <tbody id='PO7mo'></tbody>
              <tfoot id='PO7mo'></tfoot>

                本文介绍了无法解析的依赖解析[参数#0 [&lt;required&gt;$名称]]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                警告:这个问题是 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.

                这篇关于无法解析的依赖解析[参数#0 [&lt;required&gt;$名称]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:单个 Route::get() 中的多个路由调用 Laravel 4 下一篇:Laravel 4 中 File::mime() 的替换(从文件扩展名中获取 mime 类型)

                相关文章

                <small id='Zs3GY'></small><noframes id='Zs3GY'>

                  <bdo id='Zs3GY'></bdo><ul id='Zs3GY'></ul>

                  <tfoot id='Zs3GY'></tfoot>

                1. <legend id='Zs3GY'><style id='Zs3GY'><dir id='Zs3GY'><q id='Zs3GY'></q></dir></style></legend>
                2. <i id='Zs3GY'><tr id='Zs3GY'><dt id='Zs3GY'><q id='Zs3GY'><span id='Zs3GY'><b id='Zs3GY'><form id='Zs3GY'><ins id='Zs3GY'></ins><ul id='Zs3GY'></ul><sub id='Zs3GY'></sub></form><legend id='Zs3GY'></legend><bdo id='Zs3GY'><pre id='Zs3GY'><center id='Zs3GY'></center></pre></bdo></b><th id='Zs3GY'></th></span></q></dt></tr></i><div id='Zs3GY'><tfoot id='Zs3GY'></tfoot><dl id='Zs3GY'><fieldset id='Zs3GY'></fieldset></dl></div>