Adding Helper Files in Laravel 5

  1. Create a folder in your “app” directory, name it “Helpers.
  2. Then within your Helpers folder, add your function files.
    Name your files anything you like.
    Having a folder with multiple files allows us to avoid one big file that gets
    too long and unmanageable.
  3. Open your command line then navigate to your project ROOT folder
    then create a service provider called “HelperServiceProvider.php” by
    running the php artisan command:

    1
    php artisan make:provider HelperServiceProvider
  4. Open the file: app/providers/HelperServiceProvider.php
    Update the “register” method with the following snippet:

    1
    2
    3
    4
    5
    6
    public function register()
    {
        foreach (glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
    }
  5. Lastly, register the service provider in your config/app.php in the providers array
    1
    2
    3
    'providers' => [
        'App\Providers\HelperServiceProvider',
    ]

    Now any file in your Helpers directory is loaded, and ready for use.