Add helper as a ServiceProvider , for example AppServiceProvider ! Then you can register your helper file throw the register function ! Let me show you the way !
1, create your helper function :
create floder ‘Helpers’ in ‘app’ , then add a file name myHelper.php widthin content :
if (! function_exists('myHelperFunc')) { function myHelperFunc($text){ return "myHelperFunc"; } }
2, create helper provider :
add a file name HelperServiceProvider.php in ‘app/Providers’ widthin content :
namespace App\Providers; use Illuminate\Support\ServiceProvider; class HelperServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // } /** * Register any application services. * * @return void */ public function register() { foreach (glob(app_path().'/Helpers/*.php') as $filename){ require_once($filename); } } }
3, auto load your helper provider
add one line to ‘config/app.php’ file , near ‘App\Providers\AppServiceProvider::class’ :
App\Providers\HelperServiceProvider::class
4, OK , you can use your helper function myHelperFunc now, just check by yourself
Leave a Reply