Creating custom helper functions in Laravel

Creating custom helper functions in Laravel

Hello again! today we are going to learn about helper function in laravel and also how to create your own helper functions.

Laravel provides many excellent helper functions that are convenient for doing things like working with arrays, file paths, strings, and routes, among other things like the beloved dd() function.

You can as well define your own helper functions for your Laravel applications and PHP packages, by using Composer to import them automatically. If you are new to Laravel or PHP,  don't worry, we would walk you through how you might go about creating your own helper functions that automatically get loaded by Laravel.


Laravel and PHP also provide some basic functions that can be called anywhere, however, there are some times we might need to write our own custom functions that we will need both in the controller and in the views or other parts of our web application.

Creating a Helpers file in a Laravel App

The first step is to include your helper functions is within the context of a Laravel application. Based on your preference, you can choose the location of your helper file(s) however you want, however, here are a few suggested locations:

  • app/helpers.php
  • app/Http/helpers.php

in this example we would be making use of the second option.

Next we would go to our directory and create a folder called Helpers in app > Helpers

create file functions.php
<?php
function adminAsset($assetLink)
{
return asset('admin/'.$assetLink);
}
?>

Explanation: in this example, we are trying to define our own part for the items in the public folder we want to use in our blade file, example includes style and images. the normal convention is {{ asset('admin/your path')}}we want re-write it not to bother including the admin/ before paths.

Step two: Autoload the function

locate your composer.json, under the autoload section after the psr-4: then declare the following

"files": [
"app/Helpers/functions.php"
]
"autoload": {
   "classmap": [
       "database/seeds",
       "database/factories"
   ],
   "psr-4": {
       "files": [
		"app/Helpers/functions.php"
		],
   }
},
"autoload-dev": {
   "psr-4": {
       "Tests\\": "tests/"
   }
},

afterwards, you can run composer dump-autoload on your terminal. then you are good to go. You can use this helper function we created in your blade file as {{ adminAsset('path-to-tour-file-here') }}the admin/ part would be automatically attached when this function is called.

Example 2: Active Menu and Sidebar Helper functions

function isActive($url)
 {
    return Request::is($url) ? 'active' : '';
 }
 function isMenuOpen($url)
 {
    return Request::is($url) ? 'menu-open' : '';
 }

What this function does is it helps us to make a menu item active when user is in that particular url. to use thsis function in the blade file:

<li class="nav-item has-treeview {{ isMenuOpen('branch/*')}}">
            <a href="#" class="nav-link {{ isActive('branch*') }}">
              <i class="nav-icon fas fa-code-branch"></i>
              <p>
                Branch Management
                <i class="fas fa-angle-left right"></i>
              </p>
            </a>
            <ul class="nav nav-treeview">
              <li class="nav-item">
                <a href="{{ route('branch.view')}} " class="nav-link {{ isActive('branch') }}">
                  <i class="far fa-circle nav-icon"></i>
                  <p>View all Branch</p>
                </a>
              </li>
              <li class="nav-item">
                <a href="{{ route('branch.add')}}" class="nav-link {{ isActive('branch/add') }}">
                  <i class="far fa-circle nav-icon"></i>
                  <p>Add new Branch</p>
                </a>
              </li>
            </ul>
          </li>

what {{ isMenuOpen('branch/*')}}does is that it make menu item in the sidebar active while {{ isActive('branch/') }}makes the list item active. hopefully that makes sense. this is just a simple way of determining the active sidebar or navbar item.

 

 

Enjoy this article ? drop a comment below and add your own contribution.

Maye Jacob

Jacob Olorunmaye

Verified Expert
Passionate Backend Developer | Expert in PHP, Laravel, JavaScript, and Golang 🚀 | Crafting seamless, scalable solutions for the digital era

Leave a Reply

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