How to add a Global function in Laravel using Composer?

How to add a Global function in Laravel using Composer?

I recently posted an article explaining how to format a number to Indian Rupee format in PHP / Laravel,  Now this function like any other PHP class can be used inside a Laravel project by manually importing and name-spacing it in a class but it would be accessible only in the class you are using it. Today I will show you how to add any function as a global function in Laravel so that you may access it in any of the class or Controller or View you require.

The Function which we are going to make global in Laravel:

This function basically takes a number as input and returns the result as a string formatted to Indian Rupee format. See this article for more details on how this function works. Now let us see how we can add this as a global function in Laravel

Using composer to add a global function in Laravel:

  1. Create a Helpers directory in your app directory.

    It is a common practice in Laravel to create a Helpers directory in order to store all your custom classes and such functions which might not be appropriate in a Controller or View.
  2. Copy the IND_money_format() into a Indian_currency_format.php or you can also download the Indian_currency_format.php from my GitHub Gist and save it to Helpers directory.
  3. In your composer.json file add Indian_currency_format.php to files attribute of autoload property so that it can be automatically loaded by composer when your app bootstraps. This is done by composer using PSR-4 auto-loading standard.

  4. Finally, run composer dump-autoload to refresh the autoload cache.


Now you can use Indian_currency_format() function in any of your Controllers, Views or even custom classes!.

What do you think? Please share your thoughts or ask any questions you might have in the comment section below!!


You can also consider making a small donation to support me. Your donation will directly contribute to the running cost of this website and hopefully my college too 🙂

Paypal: | UPI: [email protected]

  • 126

Leave a Reply

5 Comment threads
3 Thread replies
Most reacted comment
Hottest comment thread
6 Comment authors
Dansk TerryJeremysapneshnaikPabloAnh Tran Recent comment authors
newest oldest most voted
Notify of

Thanks for this short nice tutorial. Cristal clear, now I can use my own functions in my application. Thanks a lot.

Anh Tran

I think with the number of helper functions growing, creating a file “helpers.php” to store them all is better than putting each function in its own file.


Call to undefined function DateToday()


This is exactly what I was looking for. Thanks!

Dansk Terry
Dansk Terry

Decimals are not correct. It’s late but it out on the internet so please correct it. 😀
.50 dis diaplayed as .5, .4 is displayed as .39. weird. thanks