Home >Backend Development >PHP Tutorial >How to Create and Use Custom Helper Functions in Laravel?
Defining Custom Helper Functions in Laravel
In Laravel, you may encounter repetitive code across different views that performs text formatting tasks. To alleviate this, it's beneficial to create custom helper functions that can be invoked globally.
To define such helpers, follow these steps:
"files": [ "app/helpers.php" ]
function fooFormatText($text) { // Perform text formatting operations return $formattedText; }
<p>Foo Formated text: {{ fooFormatText($text) }}</p>
Alternative Location for Helpers File:
If you prefer to keep your helpers separate from the app directory, you can place the helpers.php file in the bootstrap directory. However, don't forget to update the "files" section in your composer.json file to point to the new location:
"files": [ "bootstrap/helpers.php" ]
The above is the detailed content of How to Create and Use Custom Helper Functions in Laravel?. For more information, please follow other related articles on the PHP Chinese website!