How to create custom helper functions in Laravel 5

Reading Time: 2 minutes

Creating simple PHP helper functions in Laravel 5 is not really something that comes out of the box, unlike what is obtained in CodeIgniter. This is one of the very few things I initially missed when I switched from CodeIgniter to Laravel.

However, there is a short solution. This might not necessarily be in compliance with Laravel’s best practices, but I kinda like it. Besides, it’s pretty straight forward.

Follow these three steps and you are good to go.

1. Create a Helper’s folder

Go to your app folder, create a new folder and name it Helpers. Helpers should be a sub folder of the app folder. This folder will be where all your helper function files will reside.

You can group related functions in the same script. This is one more reason I like this approach rather than creating one long file with all the helper functions (which will get harder to maintain as you add more functions)

2. Create a Service Provider

Create a Laravel Service Provider to handle your helper scripts and functions. You can do that manually or just use the artisan CLI command.

php artisan make:provider HelperServiceProvider

In the above, the Service Provider is called HelperServiceProvider.

Service Providers in Laravel have two methods ie the boot() and the register() methods. We are going to leave the boot() method empty, we only need the register() method. Add the following code to the register() method of the Service Provider you created.

public function register()
{
    foreach (glob(app_path().'/Helpers/*.php') as $filename){
        require_once($filename);
    }
}

3. Register your Service Provider

The last step involves registering your Service Provider in your application’s app config file ie config/app.php

Got to the providers section/element of the array and add the following

App\Providers\HelperServiceProvider::class,

So your providers section should look like this

'providers' => [
    //
    // Other Service providers listed here...
    //

    App\Providers\HelperServiceProvider::class,
];

Next steps

You can now start creating your functions in files in the Helpers sub folder of the app folder. You don’t have to follow any rules in naming your files. But I like to end mine with Helper.php so as to make them easily searchable for me.

Note that the helper files are just simple PHP scripts and not classes. So you can create a new function using the following syntax

if(!function_exists('exampleFunction')){
    /**
     * Doc block of function defined
     * @param type $symbol
     * @return 
     */
    function exampleFunction($param){
        //
        //Function code here
        //
    }
}

And whenever you want to call your functions, just do it the normal PHP way ie exampleFunction()

That’s all folks!

 

 

Note:

  • This code was tested with Laravel 5.2
  • You can change the name of the Service Provider file and that of the helper folder, it will still work so long as you are consistent.
 

Afolabi 'aphoe' Legunsen

A software developer with 9 years professional experience. Over the years, he has built and contributed to a number of projects which includes MoneyTalks that won Visa's Financial Literacy Challenge and Opomulero that won the British Council's Culture Shift III. He currently the Software Project Lead of Itquette Solutions where his team has built Smart PMS and BlueQuiver HRMS among many other products.

 

Leave a Reply

Your email address will not be published. Required fields are marked *