Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am working on Laravel 5.1 project and have developed a lot of helpers.

Is there any way to automatically register helpers class in ServiceProivder in stead of adding them manually?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
316 views
Welcome To Ask or Share your Answers For Others

1 Answer

I have worked on it and I finally fixed it by putting different puzzles together ending with this solution:

For Laravel 5:

Step 1. Created folder app/Helpers

Step 2. In app/Providers folder, create provider HelpersServiceProvider.php using following artisan command:

php artisan make:provider HelpersServiceProvider

Step 3. In HelpersServiceProvider.php file, we make a foreach loop inside register function to fetch all helpers classes like this:

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

Step 4. In config/app.php added following line

/*
* Application Service Providers added by developer...
*/
AppProvidersHelpersServiceProvider::class,

That is it, the solution here is tested and works on all versions of Laravel 5.x. Now you can add unlimited helpers in helpers folder, they will be automatically added to the system.

Laravel 4 is not tested yet, but if some body do it, please add/edit this for Laravel 4.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...