How to Register Helper Functions in Laravel?

How to Register Helper Functions in Laravel?

Introduction to Helpers

We have seen many helper functions in Laravel as collect, compact, dd, auth, abort, etc. These functions make developer life easier. We can eliminate writing class paths or import them into the files. Even using helper functions in blade files are easier.

Why Helper Functions?

  • Simple Syntax – Using helper functions makes code look simple and clean.
  • Developer Experience – While creating new packages. providing such helper functions increases the developer experience.
  • Blade File Friendly – Writing complex logic can be handled easily by helper functions. (example – asset, route, etc.)

Types of Helpers

Following are the types of helpers we can see in Laravel.

  1. Helper Function – simple function without class name.
  2. Helper Class – multiple functions of common use combined in a class.

You can check available helpers in Laravel’s official documentation

Ways to write Helpers Function

Let’s create a helper functions file. You can place this file at any location in the Laravel application directory.


<?php

// app/helpers.php

if (! function_exists('your_custom_function')) {
    function your_custom_function() {
         // write your custom logic here
    }
}

Using Composer File

In this method, we are using Composer's a built-in way to autoload files. If you visit Laravel’s composer.json file, you can see many helper files included using this method.


// composer.json

{
    ...
    "autoload": {
        ...
        "files": [
            "app/helpers.php"; // location of your helper file relative to composer.json.
        ],
        ...
    }
    ...
}

Using Service Provider

To register a helper file, you can create a new service provider or add to any existing service provider. If you create a new service provider, do not forget to register the same.


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    ...
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        ...
        // require_once <location of your helper file>.
        require_once base_path() . '/app/helpers.php';
        ...
    }
    ...
}

Using Helper Class

The helper classes are normal classes usually having static methods. These are generally created for the group of functions having similar contexts. Example Illuminate\Support\Arr class having Arr::first(), Arr::has($key), etc. methods;


<?php

namespace App\Helpers; // You can use any namespace

class Helper
{
    ...
    public static function your_custom_function()
    {
        // write your custom logic here
    }
    ...
}

Conclusion

Helper functions ease the developer experience. We can write helper functions in multiple ways. We have discussed these ways in detail. With the above examples you can create the helper functions for your Laravel Applications / Packages.

If you want to read more on Laravel, visit our blogs related to Laravel.