vincent15000's avatar

Create a global helper function

Hello,

I used the mercuryseries/flashy package to display toasts on the page, but it's not compatible with Laravel 12. I tried some existing packages and I don't find any toaster package similar to the previous one.

So I decided to create my own toaster and it's a good exercise because I don't know how to do.

I'd like to display a notification on the screen after a store / update action like this.

flash()->success($message);

So the first step is to create the flash() function.

I have tried this in the AppServiceProvider.

public function register()
{
    $this->app->singleton('flash', fn () => new FlashHelper);
}

public function boot()
{
    if (! function_exists('flash')) {
        function flash(): FlashHelper
        {
            return app('flash');
        }
    }
}

And I have also created the FlashHelper class.

When I try to use the flash() function, I get an error saying that the function is undefined.

Can you help me understand how I can do what I need ? Or perhaps you have a documentation to share with me ?

Thanks a lot ;).

V

0 likes
8 replies
LaryAI's avatar
Level 58

Certainly! You're very close to having a working global helper—the problem is due to scoping and timing of global function definitions. In Laravel, to properly define a global helper, do not define it in service provider methods (like AppServiceProvider@boot). Instead, put it in a PHP file that is loaded before any other application logic and outside any class/method definition.

Step-by-step Solution:


1. Create a Helper File

Create a new file in your project, for example:

app/Helpers/helpers.php

Add your flash() function there:

<?php

if (! function_exists('flash')) {
    function flash(): \App\Helpers\FlashHelper
    {
        return app('flash');
    }
}

2. Register the Helper File in Composer

Edit your composer.json to autoload this file:

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

Then, run:

composer dump-autoload

3. Register the FlashHelper in the Service Container

What you did in your AppServiceProvider is perfect for binding the class:

public function register()
{
    $this->app->singleton('flash', fn () => new \App\Helpers\FlashHelper);
}

No need to put anything in the boot() method for this purpose.


4. Your FlashHelper Class

Your FlashHelper code is great! For completeness, make sure it's in the correct namespace/file:

File: app/Helpers/FlashHelper.php


5. Usage

Now, anywhere in your Laravel code (controllers, views, etc), you can call:

flash()->success('Record saved successfully!');

6. Display the Flash Message in Your Blade View

In your Blade template:

@if(session('flash'))
    <div class="alert alert-{{ session('flash.type') }}">
        {{ session('flash.message') }}
    </div>
@endif

Summary

  • Define helpers in a regular PHP file, not a provider.
  • Register the helper file with Composer's autoload.
  • Bind your helper class in a service provider (as you did).

This matches the pattern used by many Laravel packages and is the cleanest approach.


References:

Let me know if you get stuck!

vincent15000's avatar

I have applied all what Lary suggested me, but it doesn't work.

I still have the error message saying that the flash() function is undefined.

Shivamyadav's avatar
Level 20

Laravel expects helpers to be autoloaded, not dynamically declared.

Did you added the helper class to the composer.json Or you may forget to run the command

composer dump-autoload
1 like
vincent15000's avatar

I have done exactly like Lary suggested me, so yes I have done that ;).

vincent15000's avatar

I am tired today.

I just not copied the right line.

"files": [
    "app/helpers.php"
],

instead of

"files": [
    "app/Helpers/helpers.php"
],

Please or to participate in this conversation.