Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice to use helpers in Laravel

Tags:

php

laravel

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
like image 733
Mohammed Tawfik Avatar asked Nov 17 '25 11:11

Mohammed Tawfik


2 Answers

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.

like image 124
Hardik Savani Avatar answered Nov 20 '25 00:11

Hardik Savani


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.

like image 42
Alexey Mezenin Avatar answered Nov 20 '25 00:11

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!