Adding Helper Files in Laravel 5
- Create a folder in your “app” directory, name it “Helpers.
- 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. - 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:1php artisan make:provider HelperServiceProvider - Open the file: app/providers/HelperServiceProvider.php
Update the “register” method with the following snippet:1
2
3
4
5
6public 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 1
2
3'providers' => [
'App\Providers\HelperServiceProvider',
]Now any file in your Helpers directory is loaded, and ready for use.
Do you need help with a project? or have a new project in mind that you need help with?
Contact Me