Laravel 9 Autoload Custom Helper Functions Example

Hello Artisan,

In this tutorial, I am going to show you how we can create custom helper function in Laravel 9 application. You know that when we create a Large scale application then it is good practice to create a helper function so that we can put common methods there.

So I am here to show you how to create custom laravel 9 helper function without packages. We can use package to create this Laravel 9 custom helper functions. I don't use any packages to create a custom helper function.

So from this tutorial, you will learn laravel 9 to create a global helper file. Here, I will give you some simple steps to complete how to create a custom helper function in laravel 9 application.

 

Step 1: Create functions.php File

So create a functions.php file which is going to be a global file. So create it in the following path like:

app/Helpers/functions.php

/**
 * Write code on Method
 *
 * @return response()
 */

if (! function_exists('test_helper')) {
    function test_helper()
    {
        return 'I am from codecheef';
    }
}

 

Read also: Upload Large CSV File using Queue Job Batching in Laravel

 

Step 2: Register File In composer.json

In this step, we have to put a path of helpers file,so basically open composer.json file and put following code to update that file like below:

"autoload": {

    "psr-4": {

        "App\\": "app/",

        "Database\\Factories\\": "database/factories/",

        "Database\\Seeders\\": "database/seeders/"

    },

    "files": [

        "app/Helpers/functions.php"

    ]

},

 

Now run composer dump-autoload command to make it functional in the Laravel 9 application.

 

Read also: Laravel 9 Eloquent Accessors and Mutators Example

 

Hope this laravel 9 global functions tutorial will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js
ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT