DEV Community

Alpha Olomi
Alpha Olomi

Posted on

Creating and Using Custom Helper Functions in Laravel

Introduction

Laravel offers a variety of global "helper" functions that we can leverage in our applications, such as app_path, ucfirst, auth, and env. However, did you know that you can also define your own set of helper functions for Laravel applications and PHP packages? In this blog post, we will explore how to create and use custom helper functions in Laravel.

Why Use Custom Helper Functions?

Custom helper functions provide a convenient way to encapsulate frequently used code snippets and make them reusable across your application or even in multiple projects. They promote code modularity, improve code readability, and reduce code duplication. With custom helper functions, you can extend Laravel's functionality to suit your specific needs and enhance your development experience.

Setup:

To get started, let's create a file called Helper.php in the app/Helpers/ directory of your Laravel project. This file will serve as the home for your custom helper functions.

touch app/Helpers/Helper.php
Enter fullscreen mode Exit fullscreen mode

Next, open the composer.json file and add the file path to the files key in the autoload section, as shown below:

"autoload": {
    "files": [
        "app/Helpers/helper.php"
    ],
    // rest of the auto load section
}
Enter fullscreen mode Exit fullscreen mode

After modifying the composer.json file, run the following command to update the autoloader:

composer dump-autoload
Enter fullscreen mode Exit fullscreen mode

Example

Now, let's dive into some practical examples of creating and using custom helper functions in Laravel.

Basic "hakuna_matata" Helper Function

if (! function_exists('hakuna_matata')) {
    function hakuna_matata($name = null) {
        if ($name) {
            return "Hakuna Matata, {$name}!";
        }
        return 'Hakuna Matata!';
    }
}
Enter fullscreen mode Exit fullscreen mode

With this helper function, you call hakuna_matata() anywhere in your application. Composer will automatically load the Helper.php file, and the function will be available to use.

In this example, we simply echo the phrase "Hakuna Matata" to the screen. You can also pass in a default value to the helper function, which will be returned if no value is provided.

Facade VS Helper Function

When you create a helper function, you can use it anywhere in your application. However, if you want to use it in a class, you need to import the helper file into the class. This can be a bit cumbersome, especially if you have multiple helper functions.

Note: It is recommended* to create a helper function for Facades. Facades are a great way to access Laravel's core functionality, but they can be a bit cumbersome to use. With a helper function, you can encapsulate the Facade's functionality and make it easier to use.

Example

use MyCustomFacade;

if (! function_exists('myCustomFunc')) {
    return MyCustomFacade::myCustomFunc();
}
Enter fullscreen mode Exit fullscreen mode

Now, you can use the helper function anywhere in your application. For example, you can use it in a controller:

class MyController extends Controller
{
    public function index()
    {
        return myCustomFunc();
    }
}
Enter fullscreen mode Exit fullscreen mode

Helper functions usage in Laravel Documentation

Laravel provides a variety of helper functions that you can use in your application. For example, In the Laravel documentation, you you will find a plenty helper functions used in several areas like the view helper function, the route helper function, the asset helper function, the csrf_field helper function, to name a few.

In Conclusion

Custom helper functions are a powerful tool for extending Laravel's functionality and improving your development workflow. By encapsulating frequently used code snippets, you can enhance code modularity, readability, and reduce duplication. Whether you're creating a "myCustomFunc"

Top comments (0)