How to automatically register helper class in ServiceProvider?

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

Is there a way to automatically register helper classes in ServiceProivder instead of manually adding them?

+1
source share
1 answer

I worked on this, and I finally fixed it by putting different puzzles along with this solution:

For Laravel 5:

Step 1. Created app/Helpers folder

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

 php artisan make:provider HelpersServiceProvider 

Step 3. In the HelpersServiceProvider.php file HelpersServiceProvider.php we create an internal foreach loop registration function to retrieve all helper classes as follows:

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

Step 4. The following line is added to config/app.php

 /* * Application Service Providers added by developer... */ App\Providers\HelpersServiceProvider::class, 

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

Laravel 4 has not yet been verified, but if some of its body, add / edit it for Laravel 4.

+3
source

All Articles