Create helper functions in Laravel

- Andrés Cruz

En español
Create helper functions in Laravel

In Laravel, a helper is a global scope function, which means that from any component class, controller, model, view... we can access it to trigger its corresponding functionality. Helpers are not something invented by Laravel, they exist in a multitude of other frameworks; helpers have gained more and more space in the latest versions of Laravel, creating helper functions for other processes that were previously Facade (which are considered anti-patterns), therefore, we can use the system ones or create our own as we will show in this entry.

The help functions or helpers are functionalities that we can create throughout the app that are general purpose; generally they are NOT linked to a process or business logic, if they are not more general purpose functions such as doing some process with texts, numbers, images... and in Laravel of course we can create these types of processes or help functions : for that, we have to comply with 3 simple steps:

1. Create the file with the functions

We have to create the help function somewhere in our project, usually we do it inside the app, in some directory, for example, a folder called Helpers:

namespace App\Helpers;
class Helper{
   public static function hello(string $hello){
       return "Hola $hello";

There, we define all our functions, we can even create more files with helper functions in case you want to have some order in your functions; It is important to mention that you can create as many help files in this folder (or other folders or subfolders) for your application, therefore you are not limited to a single file.

2. Register in the composer.json

Once your files or help file with your functions have been created, the next thing we have to do is register it to be able to use it globally in the project; for this, we have to register it in a rule called autoload, in the files key, place the reference to your helpers files:

"autoload": {
       "psr-4": {
           "App\\": "app/"
       "files" : ["app/Helpers/Helper.php"],
       "classmap": [

The same here, for each help file you have, you register it in the files array.

3. Refresh dependencies

With everything correctly registered, the next thing we have to do is run a composer command so that, like any other Laravel package, it indexes it into our composer startup file:

composer dump-autoload

Now with this we can use our functions in any part of our project as we see in the video attached to our in this Post.

Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.