How to create custom helper functions in Laravel 5?

How to create custom helper functions in Laravel 5?

How to create custom helper in Laravel 5?

In this tutorial, I will tell you how to create custom helper function in Laravel 5.

I usually create many helper class for MenuHelper to get menu html and also use for define payment gateway functionality.

You can also create helper function as per your need.

Here you will know how to create helper function in simple steps.

This helpfer function is mostly useful in view to format the data and return the results.

Step 1: Create Helper Class

In this step we will create helper class so first create a directory where all helper class file will be saved.

I create a directory Libraries within app directory and inside this directory we will create new General.php PHP file.

app/Libraries/General.php
  1. <?php
  2. namespace App\Libraries;
  3. class General {
  4. // function to get gravatar photo/image from gravatar.com using email id.
  5. public static function getGravatarURL($email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array())
  6. {
  7. $url = 'http://www.gravatar.com/avatar/';
  8. $url .= md5(strtolower(trim($email)));
  9. $url .= "?s=$s&d=$d&r=$r";
  10. if ($img)
  11. {
  12. $url = '<img src="' . $url . '"';
  13. foreach ($atts as $key => $val)
  14. $url .= ' ' . $key . '="' . $val . '"';
  15. $url .= ' />';
  16. }
  17. return $url;
  18. }
  19. }
Step 2: Add File Path In composer.json to dump the autoloader

In this step, we will add file path of helper class in composer.json file within autoload object.

  1. "autoload": {
  2. "files": [
  3. "app/Libraries/General.php"
  4. ]
  5. },
Step 3: Run Composer with Following command

This is last step, here we will fire a command to dump the autoloader.

composer dump-autoload

Now you are ready to use this step to create a custom helper function in your application.

Example :

  1. Route::get('/', function(){
  2. $img='<img src="'.General::getGravatarURL('ajay.agrahari09@gmail.com').'">';
  3. echo $img;
  4. });

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort - akyazı escort - arifiye escort - erenler escort - eve gelen escort - ferizli escort - geyve escort - hendek escort - karapürçek escort - karasu escort - kaynarca escort - kocaali escort - otele gelen escort - pamukova escort - sapanca escort - söğütlü escort - taraklı escort
sapanca escort hendek escort karasu escort escort sakarya sakarya escort bayan adapazarı escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort sakarya escort