Creating Custom Global Laravel Helpers

Creating Custom Global Laravel Helpers

Spread the word! Share with your fellow developers.

Laravel Helpers are very handy in Laravel application development probably you have used some of them throughout the development of your application. For instance, to get the current route with name you can use route('route name') or for application’s base URL we use url() and so on.

These helpers are really helpful but there might be a situation where you want to create your own helper function with custom logic in it and reuse them wherever you want.

In this post, I will walk you through to create your own global helper functions. So let’s start.

Custom Helper Functions using Service Providers

We can add our own custom PHP file into Laravel using a service provider. Run below command to create your own service provider:

php artisan make:provider HelperServicerProvider

Above command will generate below service provider in app/Providers folder.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServicerProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}

Now register your HelperServicerProvider in the providers array in config/app.php file like below:

App\Providers\HelperServicerProvider::class,

Now we will create a new function inside of HelperServicerProvider called loadHelpers() like below:

protected function loadHelpers()
    {
        foreach (glob(__DIR__.'/../Helpers/*.php') as $filename)
        {
            require_once $filename;
        }
    }

In loadHelpers() function we are running a loop on all files inside app/Helpers folder and including into our app.

Now call this function inside register() method of HelperServicerProvider like below:

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->loadHelpers();
    }

Your final class will look like below:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServicerProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->loadHelpers();
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {

    }

    protected function loadHelpers()
    {
        foreach (glob(__DIR__.'/../Helpers/*.php') as $filename)
        {
            require_once $filename;
        }
    }
}

Creating Helper File

Now, create a folder inside app folder called Helpers along with a file called functions.php.

Inside functions.php file, we will create a function called app_name() which will return application name.

if (!function_exists('app_name')) {

    function app_name()
    {
        return config('app.name');
    }
}

Now, app_name() is available within your application and you can use it wherever you want by simply calling it like below.

echo app_name();

Conclusion

Laravel Helpers provide the ability to create custom functions to execute your custom logic. One practical example could be, if you want to render a menu navigation bar you can create HTML markup within a function and simply call that function in the view.

4 comments on “Creating Custom Global Laravel Helpers

  1. Can you pass params to helper function, forexample make currency formatter that conver numbers to currency and ans currency character in the end?

Leave a Reply

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

*

*
*

This site uses Akismet to reduce spam. Learn how your comment data is processed.