Laravel 5.2 未找到自定义提供程序

Laravel 5.2 Custom Provider Not Found

我正在使用 laravel 5.2 并尝试创建我自己的包,所以我创建了我的包文件夹,这是我的目录:

root
-packages
--yudijohn
---composer.json
---crud-generator
----src
-----CrudGeneratorServiceProvider.php

这是我的composer.json的内容,我用composer init

{
    "name": "yudijohn/crud-generator",
    "description": "Laravel 5 Crud Generator",
    "require": {
        "php": ">=5.5.9",
        "laravelcollective/html": "5.1.*"
    },
    "license": "MIT",
    "authors": [
        {
            "name": "Yudi Yohanes Septian Gotama",
            "email": "yudijohn.sg@gmail.com"
        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-4": {
            "App\": "app/",
            "yudijohn\CrudGenerator\": "packages/yudijohn/crud-generator/src"
        }
    }
}

并在 app/Provide 添加提供商设置。php

App\Providers\PackageServiceProvider::class,
yudijohn\CrudGenerator\CrudGeneratorServiceProvider::class,

这是我的CrudGeneratorServiceProvide.php

的内容

namespace yudijohn\CrudGenerator;

use Illuminate\Support\ServiceProvider;

class CrudGeneratorServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->commands(
            'yudijohn\CrudGenerator\Commands\CrudCommand',
            'yudijohn\CrudGenerator\Commands\CrudControllerCommand',
            'yudijohn\CrudGenerator\Commands\CrudModelCommand',
            'yudijohn\CrudGenerator\Commands\CrudMigrationCommand',
            'yudijohn\CrudGenerator\Commands\CrudViewCommand'
        );
    }
}

但是当我尝试 php artisan serve 时,出现了这个错误

[Symfony\Component\Debug\Exception\FatalErrorException]                
Class 'yudijohn\CrudGenerator\CrudGeneratorServiceProvider' not found

我不知道为什么找不到 class?我必须怎么做才能解决这个错误? 请有人帮助我

这是PackageServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Symfony\Component\Finder\Finder;

use Illuminate\Filesystem\Filesystem;

class PackageServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
            $this->loadAutoloader(base_path('packages'));
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
        }

        /**
        * Require composer's autoload file the packages.
        *
        * @return void
        **/
        protected function loadAutoloader($path)
        {
            $finder = new Finder;
            $files = new Filesystem;

            $autoloads = $finder->in($path)->files()->name('autoload.php')->depth('<= 3')->followLinks();

        foreach ($autoloads as $file)
        {
            $files->requireOnce($file->getRealPath());
        }
        }

}

您想添加 "yudijohn\CrudGenerator\": "packages/yudijohn/crud-generator/src"project 根目录下的 composer.json,而不是你刚刚创建的包。它应该位于 "App\": "app/" 条目的正下方。

之后,运行 composer dump-autoload 将其添加到作曲家的自动加载 classes.

我也不相信你需要 PackageServiceProvider.php,当你使用 composer 时添加另一个自动加载器似乎是多余的。

还要确保你的文件名和 class 名称相同,你提到 CrudGeneratorServiceProvide.php 而你的 class 名称是 CrudGeneratorServiceProvider(注意末尾的 r class姓名)

这里是进一步研究的好资源:https://github.com/jaiwalker/setup-laravel5-package