How to create custom helpers in Laravel?

How to create custom helpers in Laravel?


3 min read

Laravel is one of the best frameworks out there FOR ME. I use it every day for my 9-5 job and I really love it.

Whenever I start a new project either for my job or for my side projects, the first thing that I do is to create a new file called helpers.php. I use that file to create the custom helper functions that I will be used for along the project.

For example; I really don't like to find the name of the current route in blade files following a hard way like as below;

{{ Illuminate\Support\Facades\Route::currentRouteName() }}

Instead, I prefer to create a simple function that I can use anywhere in the project to get the current route name in a shorter syntax like as below;

{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.

How to make sure that this simple function can work anywhere?

Since Laravel as a default uses composer to manage the dependencies, we have a powerful tool under our hands and we can tell the composer to autoload any file we want and it allows us to use it in the entire project.

To do so; first of all, we have to create the file anywhere we want, as a default, I always put it under the app folder.

  • app/helpers.php

After I am sure that, the file exists, I create my own custom helper functions in the file as shown below;

// function_exists method is initiated to check;
// If there is any function existed with the name of getCurrentRouteName,
// If not, this function will be added otherwise existed one will be used.
// The aim is to protect existing functions to be overwritten
if (!function_exists('getCurrentRouteName')) {
    function getCurrentRouteName()
        return Illuminate\Support\Facades\Route::currentRouteName();

At this point, I have to tell the composer to discover and autoload this file by adding it to the composer.json file as shown below otherwise my function will not be callable.

If you don't know, composer.json is a file in that you can define the project dependencies and install them using a simple command composer install or update them with composer update.

    "require": { ... },
    "require-dev": { ... },
    "autoload": {
        "psr-4": { ... },
        "files": [
            "app/helpers.php" //this is the place I tell composer to autoload file

I created a file, add it to the composer.json file, so is it going to work? Unfortunately No ๐Ÿ˜ฅ

Because I have to run a simple composer command to load all files again in the entire project just for one time.

composer dump-autoload

I am afraid to ask, but is it going to work? Yes! Congratulations, you've made it ๐Ÿ˜‡

So, now I can use getCurrentRouteName anywhere I want as I showed in the example above.

{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.

This is how I create custom helper functions in my Laravel projects.

Did you find this article valuable?

Support Olgun by becoming a sponsor. Any amount is appreciated!