"无法解决的依赖性解析"错误,Laravel服务提供商

时间:2015-11-06 02:30:55

标签: php laravel laravel-5 package laravel-5.1

我正在尝试构建自定义服务提供程序包,但是我遇到了以下错误。有没有人有这方面的经验?

Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider

包文件夹结构:

[root]
....packages/
........mbarwick83/
............previewr/
................src/
....................PreviewrServiceProvider.php
....................Previewr.php
................composer.json

配置/ app.php:

Mbarwick83\Previewr\PreviewrServiceProvider::class

服务提供商:

<?php

namespace Mbarwick83\Previewr;

use Illuminate\Support\ServiceProvider;

class PreviewrServiceProvider extends ServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = false;

    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any package services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('Mbarwick83\Previewr\Previewr',function($app){
            return new Previewr($app);
        });
    }
}

Previewr.php(class):

<?php

namespace Mbarwick83\Previewr;

class Previewr
{
    /**
     * Create a new Previewr Instance
     */
    public function __construct()
    {
        //
    }

    /**
     * Friendly welcome
     *
     * @param string $phrase Phrase to return
     *
     * @return string Returns the phrase passed in
     */
    public function something($phrase)
    {
        return $phrase;
    }
}

Controller/view:

use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;

public function index(Previewr $previewr)
{
    echo $previewr->something('Hello, League!');
}

composer.json:

"autoload": {
        "psr-4": {
            "Mbarwick83\\Previewr\\": "packages/Mbarwick83/Previewr/src"
        }
    },

1 个答案:

答案 0 :(得分:1)

在您的控制器中,您确定要注入服务提供商吗?

use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;

您可能希望使用此功能:

use Mbarwick83\Previewr\Previewr;