Best practices for custom helpers on Laravel 5

Published 3 years ago by calebeoliveira

Hi, I would like to create some helpers (functions) to avoid repeating code between some views, in L5 style:

Formated text: {{ fooFormatText($text) }}

They are basically text formatting functions. Where and how can I put a file with these functions?

JeffreyWay
  1. Within your app/Http directory, create a helpers.php file and add your functions.
  2. Within composer.json, in the autoload block, add "files": ["app/Http/helpers.php"].
  3. Run composer dump-autoload.

That should do it. :)

mineass

Call to undefined function

bestmomo
bestmomo
3 years ago (378,310 XP)

@menieass

If your composer "files" autoload option is correct you should find your reference in vendor/composer/autoload_files.php. Check it.

pmall
pmall
3 years ago (583,445 XP)

You can also include the helper file in the boot method of your AppServiceProvider

racl101

Just tried Jeffrey's way (no pun intended) and it worked for me on Laravel 5.

stefanbauer

Another question to that related topic. Is there a rule when to use easy "php"-helpers like in the above examples, or when to create a blade extension? Both would end in the same result. But is there something when to do what?

luanrodriguesp

I did the same way that @JeffreyWay said and i'm getting "Call to undefined function"

here is my helpers.php


php namespace App\Http;
class Helpers {
    public function FixData($data){
        $d = explode("-", $data);
        return $d[2]."/".$d[1]."/".$d[0];
    }
}

OBS: To post here, I have removed the "<\?" from the first line, but in the original file its ok.
alanablett

@luanrodriguesp

Jeffs method includes a file containing functions only, not a class. SO your file should look like this:

public function FixData($data){
    $d = explode("-", $data);
    return $d[2]."/".$d[1]."/".$d[0];
}
bestmomo
bestmomo
2 years ago (378,310 XP)

@luanrodriguesp if you want to use classes you can create a service in app/Services with good namescpace and it'll be autoloaded.

martinbean

@stefanbauer What if you need to use a helper outside of a Blade template? What if you switch your template engine? You then have to re-write your Blade extensions into whatever template engine you’re switching to.

tjans
tjans
2 years ago (1,930 XP)

I was getting the same undefined error until I RTFM, and instead of adding the files piece to the bottom, I added it to the autoload block as the instructions said and voila, it worked!

Vasek18

Did everything like Jeffrey said, but still got "Call to undefined function" Laravel 5.2 From composer: "autoload": { "classmap": [ "database" ], "psr-4": { "App\": "app/" }, "files": [ "app/helpers.php" ] },

From autoload_files.php: 'b4e3f29b106af37a2bb239f73cdf68c7' => $baseDir . '/app/helpers.php',

Update The problem was in php brackets. I used <? instead of <?php

jlrdw
jlrdw
2 years ago (222,990 XP)

@mineass did you properly namespace your helper?

donpuerto

@vasanth it should work, take note, you will allow only function.

yes

function ($x, $y){
    //return blah blah;
}

no

public function ($x, $y){
    //return blah blah;
}
Bespired

In app/Http/helpers.php

<?php

function str_spacecase($slug)
{

    return str_replace("_", " ", $slug);

}

in app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
        require_once __DIR__ . '/../Http/helpers.php';
    }
}

Please sign in or create an account to participate in this conversation.