I would like to create helper functions to avoid repeating code between views in Laravel. For example:
view.blade.php
<p>Foo Formated text: {{ fooFormatText($text) }}</p>
They're basically text formatting functions. How should I define globally available helper functions like fooFormatText()?
Create a
helpers.phpfile in your app folder and load it up with composer:After adding that to your
composer.jsonfile, run the following command:If you don't like keeping your
helpers.phpfile in yourappdirectory (because it's not a PSR-4 namespaced class file), you can do what thelaravel.comwebsite does: store thehelpers.phpin the bootstrap directory. Remember to set it in yourcomposer.jsonfile: