How to Create Your Own Laravel Helpers

December 18th, 2024 • 3 minutes read time

How can you add your own helper functions to Laravel? It's pretty easy, and this guide will get you started.

To start, create an app/helpers.php file. In fact, you can put this anywhere in your project, but this is where I typically place it.

<?php

// Nothing here yet!

We'll define all of our helper functions in this file. Later on, nothing stops you from splitting these into multiple files.

Our app/helpers.php file exists but isn't known by our application yet since it's not loaded anywhere.

To load this, we'll lean on Composer. In your composer.json file, add a files array to the autoload section and add the location of your helpers.php file:

"autoload": {
    "files": [
        "app/helpers.php"
    ],
    //...
},

Once that's added, dump the autoloader for Composer. This will take into account the changes you just made to composer.json:

composer dump-autoload -o

Great news — your functions file is now loaded within your application, and we can start defining functions!

I recently worked on a project where I needed a team function to read the currently authenticated user's current team, so let's roll with that example.

Whatever function you want to define, just do so as a normal PHP function:

function team() {
    return request()->team();
}

And just like that, you now have access to a team function in your project (no need to run dump-autload on Composer every time you make a change here).

An important part of defining functions in PHP is that they'll override each other, depending on which order you define them.

The last thing we want to do is override a function that exists either:

  1. Within the Laravel framework
  2. Within a package we've installed

So, best practice (using the term lightly) would be to wrap our function definition in an IF statement to check if it already exists:

if (!function_exists('team')) {
    function team() {
        return request()->team();
    }
}

If you do this for every function you define, you're less likely to break something else, and you'll simply have to choose a different function name if it's already taken.

You have a couple of options if any of your functions require access to Laravel's container or other framework-related areas.

In the example above, we used an already-defined Laravel request helper to access the Request object. Here are a few other ways to get what you need:

  • app('request') will resolve the Request object from the container. You can use the app function (yep, another helper) to resolve almost anything you need by name or interface.
  • If you're dealing with routes inside your own functions, you can use the route helper.
  • To access the currently authenticated user within your helpers, you can either use the auth()->user() method, or app(AuthFactory::class)->user().

There are plenty of other ways too. I'd recommend checking out the helpers.php file within the Laravel framework to find out how this is used internally.

That's it! A super simple way to create your own helper functions in Laravel.

If you found this article helpful, you'll love our practical screencasts.
Author
Alex Garrett-Smith
Share :

Comments

No comments, yet. Be the first!