Search code examples
phplumenphp-carbon

How to enable the helper now in Lumen


I'm new to Lumen (version 7) and then i'm trying to use carbon as usual like in Laravel especially the helper now but I'm getting this error

Call to undefined function App\Http\Controllers\now()

Thanks in advance


Solution

  • If you look for function now in the vendor of a Laravel app, you would find it's located in src\Illuminate\Foundation\helpers.php and as Lumen aims to be lighter (and faster) than Laravel so it comes with fewer tools than Laravel, such file loaded for each request to provide functions shortcuts is the kind of small bottleneck that have been sacrificed.

    You still can create your own helper in any global place:

    function now($timezone = null)
    {
        return Carbon::now($timezone);
    }
    

    It would be equivalent.

    Else, calling Carbon::now() will make explicit that you get a Carbon instance. I would say it's not so bad to type it for each new instance you created.