Laravel Custom Helper Functions File

In this article, We will discuss how to add in Laravel custom helper functions files. We know that Laravel provides lots of inbuilt features. But sometimes we need to create some reusable functions, which contains as required valuable logic. That functions reduce the development time frame.

In Laravel, it’s easy to add custom helper functions file in Laravel. Please follow step given below:

Step 1: Create one file named “helper.php” under “app” directory.

First, we are creating one helper file named “helper.php” in “app/helper.php” path. And add some logic as per the requirement. For example, we are creating a function which returns an array which contains the weekdays.

<?php

if(!function_exists('weekdays')) {
  function weekdays() {
    return [
      'Mon' => 'Monday',
      'Tus' => 'Tuesday',
      'Wed' => 'Wednesday',
      'Thu' => 'Thursday',
      'Fri' => 'Friday',
      'Sat' => 'Saturday',
      'Sun' => 'Sunday'
    ];
  }
}

?>

In this example, we are using PHP default function “function_exists”. It returns TRUE if the given function has been defined. You can found more details here on this function.

Step 2: Add “app/helper.php” file path in the “composer.json”.

Now, it’s time to add app/helper.php file path in composer.json located at application root. We are adding this path in the autoload section.

Why we are doing this. Because Laravel not detects our helper file automatically. We need to add this file in Laravel so our application detects and load all our helper function in the application.

{
  ...
  "autoload": {
    "classmap": [
      ...
    ],
    "psr-4": {
      ...
    },
    "files": [
      "app/helper.php"
    ]
  },
  ...
}

After updating the composer.json. Now, we are executing the given command.

composer dump-autoload

This command won’t download anything new but looks for all of the classes it needs to include again. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php).

Step 3: How to use the helper function in Controller or Views

Everything is ready now, You can use the helper function in controller or view. No need to include the helper file individually in controller or view.

Controller example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
  public function index()
  {
    $weekdays = weekdays();

    $result = '';

    // Other Logics 

    return view('home', compact('result'));
  }
}

View example

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            @php
                $weekdays = weekdays();

                print_r($weekdays);
            @endphp
        </div>
    </div>
</div>
@endsection

In this article, we discuss how to add custom helper function file in Laravel. Hope you like this tutorial, please feel free to comment for any query.

 

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

ComposerLaravelLaravel Helper
Comments (0)
Add Comment