Laravel 5 Custom Helper

Download: Laravel 5 Custom Helper Tutorial Files

Introduction

Laravel is a powerful framework with most common web tasks built into it. However, they will be times when you will want to add your own functionality to the framework. You can achieve this via customer helpers.

When you call asset() and pass in a parameter, that’s a built in function that you are dealing with. Helpers are functions that perform common tasks and can usually be called from anywhere in the framework. This tutorial will show you how you can create your own custom helpers

Laravel 5 custom helper

Topics to be covered

We will cover the following topics in this tutorial

  • Custom helpers’ directory
  • Helper class definition
  • Helpers service provide class
  • Helper class alias

Tutorial project

Run the following composer command to create a new Laravel project

composer create-project laravel/laravel larahelpers

Custom helpers’ directory

Our helpers will be located in the /app directory

Create a new directory Helpers in /app/Helpers

Helper class definition

Let’s now create a simple helper function that will concatenate two strings. Create a new file MyFuncs.php in /app/Helpers/MyFuncs.php Add the following code

<?php

namespace App\Helpers;

class MyFuncs {

    public static function full_name($first_name,$last_name) {
        return $first_name . ', '. $last_name;   
    }
}

HERE,

  • namespace App\Helpers; defines the Helpers namespace under App namespace
  • class MyFuncs {…} defines the helper class MyFuncs
  • public static function full_name($first_name,$last_name) {…} defines a static function that accepts two string parameters and returns a concatenated string

Helpers service provide class

Service providers are used to auto load classes. We will need to define a service provider that will load all of our helper classes in /app/Helpers directory.

Run the following artisan command

php artisan make:provider HelperServiceProvider

The file will be created in /app/Providers/HelperServiceProvider.php

Open /app/Providers/HelperServiceProvider.php

Add the following code

<?php 

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperServiceProvider extends ServiceProvider {

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

   /**
    * Register the application services.
    *
    * @return void
    */
   public function register()
   {
        foreach (glob(app_path().'/Helpers/*.php') as $filename){
            require_once($filename);
        }
   }
}

HERE,

  • namespace App\Providers; defines the namespace provider
  • use Illuminate\Support\ServiceProvider; imports the ServiceProvider class namespace
  • class HelperServiceProvider extends ServiceProvider {…} defines a class HelperServiceProvider that extends the ServiceProvider class
  • public function boot(){…} bootstraps the application service
  • public function register(){…} is the function that loads the helpers
  • foreach (glob(app_path().'/Helpers/*.php') as $filename){…} loops through all the files in /app/Helpers directory and loads them.

Helper Service Provider and class alias configuration

We now need to register the HelperServiceProvider and create an alias for our helpers.

Open /config/app.php file

Locate the providers array variable

Add the following line

App\Providers\HelperServiceProvider::class,

Locate the aliases array variable

Add the following line

'MyFuncs' => App\Helpers\MyFuncs::class,

Save the changes

Using our custom helper

We will create a route that will call our custom helper function Open /app/routes.php

Add the following route definition

Route::get('/func', function () {
    return MyFuncs::full_name("John","Doe");
});

HERE,

  • return MyFuncs::full_name("John","Doe"); calls the static function full_name in MyFuncs class

Load the following URL in your browser

http://localhost/larahelpers/public/func

You will get the following result

Laravel 5 custom helper

Summary

Custom helpers can be used for commonly performed tasks. They are mostly helpful in views where you do not want to include business logic. You can create custom functions that format or process the data and return the results

What’s next?

If you found this tutorial useful, then share it on social media to support us. You can also give us feedback via the comments section. Happy coding and sharing.

Tutorial History

Tutorial version 1: Date Published 2015-09-16

Related Tutorials