Thursday , 2 July 2020
Stackode
Home > Laravel > Make your own Helper Functions – Laravel

Make your own Helper Functions – Laravel

How Make your own Helper Functions.? Well, we just see how and what should be made.

The structure of the folders that I use to create a “Helper Functions”.

helper-functions-blog-whendy
helper-functions-blog-whendy

Next we create a file “Helpers.php” and saved in the folder “app/Helpers“. In this Helpers.php file we put the functions that later we will call / use in our every need.

Example :

<?php

function AddClassActiveRoute($routes, $output = "active")
{
    if(is_array($routes)){
        foreach ($routes as $route)
        {
            if (Route::currentRouteName() == $route) return $output;
        }
    }
    if (Route::currentRouteName() == $routes)
        return $output;

}

This examples of functions that I created to add the active class.

Next, we have registered Helpers.php file that can be used. How to.? The way we have registered at the ServiceProvider.

Ok, Create a file with the name “HelperFunctionProvider.php“. Save in the folder “app/Providers“.

Follow the example of this code.

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class HelperFunctionProvider extends ServiceProvider {

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

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

}

I think you understand the purpose of the above code.

Then edit the file config/app.php, add to a providers App\Providers\HelperFunctionProvider :: class .

Well, now you’ve made Helper Functions themselves.

If you have more simply please leave your comment on Make your own Helper Functions – Laravel. Thank.

About Whendy

Whendy Blog mengumpulkan beberapa Tutorial Website, PHP, AngularJS, JQuery, Laravel, Codeigniter dan lain-lain. ( jika sempat ) :)

Check Also

tinymce-whendy-blog

TinyMCE Plugin WordPress

WYSIWYG, tentunya sudah sering kita dengar istilah ini, merupakan kepanjangan dari What You See Is ...

One comment

  1. Thanks for sharing your thoughts about Tutorial Laravel.
    Regards

Leave a Reply

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