Laravel Class 'App\Modules\ServiceProvider' not found?

前端 未结 8 1466
暗喜
暗喜 2021-01-01 19:39

Hello Friends I am new in Laravel framework.

i create modules directory in app folder.

then i also create ServiceProvider.php file in modules directory.

8条回答
  •  梦谈多话
    2021-01-01 20:05

    my initial thought was the composer autoload as well, but it didn't feel very Laravel 5ish to me. L5 makes heavy use of Service Providers, they are what bootstraps your application.

    To start off I created a folder in my app directory called Helpers. Then within the Helpers folder I added files for functions I wanted to add. Having a folder with multiple files allows us to avoid one big file that gets too long and unmanageable.

    Next I created a HelperServiceProvider.php by running the artisan command:

    artisan make:provider HelperServiceProvider or php artisan make:provider HelperServiceProvider Within the register method I added this snippet

    public function register()
    {
        foreach (glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
    }
    

    lastly register the service provider in your config/app.php in the providers array

    'providers' => [ 'App\Providers\HelperServiceProvider', ] now any file in your Helpers directory is loaded, and ready for use.

    UPDATE 2016-02-22

    There are a lot of good options here, but if my answer works for you, I went ahead and made a package for including helpers this way. You can either use the package for inspiration or feel free to download it with Composer as well. It has some built in helpers that I use often (but which are all inactive by default) and allows you to make your own custom helpers with a simple Artisan generator. It also addresses the suggestion one responder had of using a mapper and allows you to explicitly define the custom helpers to load, or by default, automatically load all PHP files in your helper directory. Feedback and PRs are much appreciated!

    composer require browner12/helpers

提交回复
热议问题