【问题标题】:Laravel 5.5 Service ProviderLaravel 5.5 服务提供者
【发布时间】:2018-02-19 17:49:29
【问题描述】:

我正在尝试使用我在整个应用程序中使用的常用方法的自定义服务提供程序。但是我得到了一个不可实例化的错误目标。我在 Laravel 5.3 中也可以正常工作,但现在在 Laravel 5.5 中无法正常工作。这是我的代码: 在 app\Helpers 文件夹中,我创建了一个带有 FrontendContracts 接口的文件夹 Contracts。

namespace App\Helpers\Contracts;

Interface FrontendContracts{
    public function randomString($len);
} 

在 app\Helpers 我有一个实现接口的类 FrontendMethods

namespace App\Helpers;
use App\Helpers\Contracts\FrontendContracts;

class FrontendMethods implements FrontendContracts{
public function randomString($len){
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmonpqrstuvwxyz";
        $result = "";
        $charArray = str_split($chars);
        for($i = 0; $i < $len; $i++){
            $randItem = array_rand($charArray);
            $result .= "".$charArray[$randItem];
        }
        $result .= time();
        return $result;
    }
}

在 app\Providers 我有 FrontendServiceProvider 类:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Helpers\FrontendMethods;

class FrontendServiceProvider extends ServiceProvider{
    protected $defer = true;

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot(){
        //
    }
    /**
     * Register the application services.
     *
     * @return void
     */
    public function register(){
        $this->app->bind('App\Helpers\Contracts\FrontendContracts', function(){

            return new FrontendMethods();
        });

    }

    /**
     * Get the services provided by the provider.
     *
     * @return array
     */
    public function provides(){
        return ['App\Helpers\Contracts\FrontendContracts'];
    }

}

我已在 providers 数组中将提供者注册为:

App\Providers\FrontendServiceProvider::class,

我收到错误消息 Unresolvable dependency resolveing [$parameter] in class {$parameter->getDeclaringClass()->getName()}

并且 Target [App\Helpers\Contracts\FrontendContracts] 不可实例化。

有人可以指出我做错了什么吗?

【问题讨论】:

    标签: laravel-5.5


    【解决方案1】:

    我认为您的FrontendServiceProvider 类中不需要provides() 方法。尝试删除它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 2015-10-24
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2019-05-29
      • 2014-04-20
      相关资源
      最近更新 更多