3

Hello I am creating a big Laravel application.

I want to create a punch of helper functions (may be 1000 functions) to be used in templates and modules within the application.

What is the best practice to do these helpers?

  1. Using helpers class and put functions inside class
  2. Using functions and autoload helper files
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
Mohammed Tawfik
  • 571
  • 1
  • 8
  • 21
  • Possible duplicate of [Best practices for custom helpers on Laravel 5](http://stackoverflow.com/questions/28290332/best-practices-for-custom-helpers-on-laravel-5) – Moppo Apr 20 '16 at 12:25

2 Answers2

5

i think you have to follow this link http://itsolutionstuff.com/post/how-to-create-custom-helper-in-laravel-5example.html for create custom helper.

First create helpers.php file in following path : app/Http/helpers.php and

Open your composer.json file and add this:

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

then

composer dump-autoload

Now you can use helper function any where.

Hardik Savani
  • 183
  • 1
  • 4
4

You can go any of these ways, but probably you'd want to create helpers.php file with all your helper functions and then autoload this file as Laravel creators did it with vendor\laravel\framework\src\Illuminate\Support\helpers.php

Good practice is to use your own class, of course. But still, since you're using Laravel you might want to use same approach as Laravel creators use.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Thanks Alex, I know how to do both ways, but I am asking what is the best practice and is one of these ways slower or may cause issues if I used in large scale? – Mohammed Tawfik Apr 06 '16 at 10:38
  • Good practice is to use your own class, of course. But still, since you're using Laravel you might want to use same approach as Laravel creators use. – Alexey Mezenin Apr 06 '16 at 10:46