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
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' => [
'AppProvidersHelperServiceProvider',
]
After that you need to run composer dump-autoload
and your changes will be visible in Laravel.
now any file in your Helpers
directory is loaded, and ready for use.
Hope this works!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…