简体   繁体   中英

How to autoload a new folder in Laravel 5 using composer.json?

I created a small app using Laravel 5.2. I kept all of my work inside a folder called "Modules" located in App/Modules .

Now, I am trying to move out my Modules folder to the root directory "outside the App folder." So my Modules folder is now located next to the App .

I want to auto load the Modules folder just like the App folder.

To auto load the Modules, I modified my autoload section of my composer.json` to look like the following:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/",
        "Modules\\": "modules/"
    }
},

Additionally, in the app.php config file I added this service provider to my "providers" array

Modules\Vendname\Surveys\Providers\CoreValidatorServiceProvider::class,

Then from the command line, I executed the following command

composer dumpautoload

Every time I go to the website, I get this error

FatalErrorException in ProviderRepository.php line 146: Class 'Modules\Vendname\Surveys\Providers\CoreServiceProvider' not found

Here is the code for my CoreServiceProvider.php file

<?php

namespace Modules\Vendname\Surveys\Providers;

use Illuminate\Support\ServiceProvider;

class CoreServiceProvider extends ServiceProvider
{

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {

        if (! $this->app->routesAreCached()) {
            require __DIR__ . '/../routes.php';
        }

    }

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

    }

}

What am I doing wrong here? What do I need to do in order to properly auto load the new folder?

I finally got it to work.

I commented out the line

Modules\Vendname\Surveys\Providers\CoreValidatorServiceProvider::class,

in the app.php file.

then in the command line I did the following commands

php artisan clear-compiled
composer dumpautoload
composer update

Then I uncommented out the line in the app.php file

Modules\Vendname\Surveys\Providers\CoreValidatorServiceProvider::class,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM