Friday, May 20, 2016

How to make helpers in Laravel 5

How to make helpers in Laravel 5:


 Many times we create some function that further used in different controllers. To avoid multiple creation of same function in multiple controller, we can write these common function in helpers.

 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,

    1. namespace App\Helpers; defines the Helpers namespace under App namespace
    2. class MyFuncs {…} defines the helper class MyFuncs
    3. 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,

    1. namespace App\Providers; defines the namespace provider
    2. use Illuminate\Support\ServiceProvider; imports the ServiceProvider class namespace
    3. class HelperServiceProvider extends ServiceProvider {…} defines a class HelperServiceProvider that extends the ServiceProvider class
    4. public function boot(){…} bootstraps the application service
    5. public function register(){…} is the function that loads the helpers
    6. 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

Add the following route definition

<?php

namespace App\Http\Controllers;

use App\Helpers\MyFuncs;

class CustomerController extends Controller
{

    public function getCustomerName($id)
    {
        $response = $this->getCustomerDetail($id);
        $msg = "Customer detail.";
        return MyFuncs::full_name($response->first_name, $response->last_name);
    }
}

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

Thanks.

No comments:

Post a Comment