Create custom Laravel Helper Methods

Create custom Laravel Helper Methods

Hi All, this is Adi, with another Laravel article. I wanted to take some time to explain what Laravel’s Helper methods are and how your can create your own ones. I was never a big fan of creating my own customer helper methods when I started with Laravel, but with time, I have started using them more often and these days there’s no Laravel project without customer helpers.

Let’s get started.

What are Laravel Helpers

Laravel Helper methods are “plain old” PHP functions, that you can call from any part of your Laravel Application. You may have already used a lot of Laravel’s built-in helper methods like route(), url(), view(), dd(), and a list of others. You do not have to require any PHP files or reference any namespace to use them, they are magically available in all parts of your application. I’ll explain more about the magic part in just a moment.

Here are a few use cases for creating custom helper methods. To call other utility classes, if there’s a generic logic that’s used multiple times in your code you might want to extract it out into a helper. I extensively use helpers to make formatting easier in my Blade view, such as automatically adding a currency symbol before a number or to format a number in a certain digit grouping or set dates in your application to use the same format on display.

Now let’s see how simple it is to create custom Helper methods.

Creating Custom Helpers

It is as simple as creating a file somewhere in the app directory and inform composer to auto-load it when the application starts, simple is it not. I usually have all my helper functions in a single file called Helpers.php in the app/Utils/ folder, this is where I store all my utility classes but you can create this file anywhere in the app directory you see fit. Then, you need to register this file in your composer.json file that’s at the root of your project. For reference you can check out how the Laravel framework registers it’s helpers here.

No alt text provided for this image

And that’s it. Make sure to run this command composer dump-autoload and restart your application. This will make sure that the Helper.php is loaded when Laravel is started. Now let’s define your first helper method in the Helper.php file.

No alt text provided for this image

This say_hi method can now be accessed in any part of your application. It’s a simple example but you can make it as complex as needed.

Conclusion

I hope you found this article on Laravel helpful. Laravel Helpers are such a handy feature that saves a lot of time. Hope you see the reasons to create your custom ones.

Useful Resources:

https://laravel.com/docs/7.x/helpers

https://laravel.com/docs/7.x/facades#facades-vs-helper-functions


Thank You
That’s all for now, this has been Adi.
If you are looking for a Freelance Web Developer you can contact me


要查看或添加评论,请登录

Adi SK的更多文章

  • My Journey with the Tech Stack ?? at Uzhavarbumi

    My Journey with the Tech Stack ?? at Uzhavarbumi

    Four years ago today, on 1/Jun/2019, I "officially" started a new freelance project. For us developers, the official…

    4 条评论
  • How to load custom JSON files into Laravel's config

    How to load custom JSON files into Laravel's config

    Hi All, this is Adi, again with another Laravel blog post. This time I wanted to share my solution to loading custom…

  • How to setup Specific Subdomain routing in Laravel

    How to setup Specific Subdomain routing in Laravel

    Hi all, this is Adi, again with another Laravel post. In one of my recent projects, I had to set up specific subdomain…

    1 条评论
  • What are Laravel Resource Controllers

    What are Laravel Resource Controllers

    Hi all, this is Adi again for a Laravel post. I recently found a lot of questions on StackOverflow asking what Laravel…

  • SimplestWeb Updated

    SimplestWeb Updated

    Hi All, I have some great news about Simplest Web. I’ve decided it’s time to take my freelance efforts to the next…

  • A recap of 2019

    A recap of 2019

    Hi all, this I Adi. 2019 has been a wonderful year for me, so I just wanted to take a moment to reflect and document…

  • How to setup Tailwind CSS with Parcel Bundler

    How to setup Tailwind CSS with Parcel Bundler

    Introduction Hi all, this is Adi. Today, I wanted to share with you my experience of how I set up Tailwind CSS with…

  • Reusing a Controller method for multiple actions in Laravel

    Reusing a Controller method for multiple actions in Laravel

    Hi All, this is Adi with another Laravel tutorial. This time, I wanted to cover my basic solution to a problem we might…

  • How to setup automatic db backup in Laravel

    How to setup automatic db backup in Laravel

    Hi All, this is Adi again with a Laravel related article. This time in a shorter format.

  • Launching Jobs by LaravelCollections.com

    Launching Jobs by LaravelCollections.com

    Hi All. I wanted to show you a service I have been working on for the past few weeks.

社区洞察