yudijohn
yudijohn

Reputation: 1278

Laravel 5.2 Custom Provider Not Found

I'm using laravel 5.2 and try to create my own package, so i creating my package folder, and this is my dir:

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

this is the content of my composer.json, i make i with 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": "[email protected]"
        }
    ],
    "minimum-stability": "dev",
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "yudijohn\\CrudGenerator\\": "packages/yudijohn/crud-generator/src"
        }
    }
}

and add a provider setting at app/Provide.php

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

and this is the content of my 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'
        );
    }
}

but when i try to php artisan serve, this error show up

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

i dont know why the class is not found? what must i do to solve this error? please someone help me

this is 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());
        }
        }

}

Upvotes: 0

Views: 1876

Answers (1)

Michael Walton
Michael Walton

Reputation: 136

You want to add "yudijohn\\CrudGenerator\\": "packages/yudijohn/crud-generator/src" to the composer.json at your project root, not the package you just made. It should go right below the "App\\": "app/" entry.

After that, run composer dump-autoload to add it to composer's autoloaded classes.

I also don't believe you need PackageServiceProvider.php, it seems redundant to add another autoloader when you're using composer.

Also make sure your filename and classnames are the same, you mentioned CrudGeneratorServiceProvide.php while your class name is CrudGeneratorServiceProvider (notice the r at the end of the classname)

Here's a good resource for further research: https://github.com/jaiwalker/setup-laravel5-package

Upvotes: 2

Related Questions