Skip to main content

Global Helpers: Methods That "Don't Fit" Anywhere Else

Premium
2:59

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 09 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

T
Taboritis ✓ Link copied!

I think, short methods as in example should not be extracted to helper classes like this. Only one benefit you have is shorter code. If you have to read this code you have to check another place in your code. Is it more readable? I don't know...

Second argument - too many static methods suggests that it is not OOP approach. It know - it looks fine, but if it has any side effects this code will be not testable/mockable.

Maybe, only maybe, it could be justified if the same lines are repeated in many places of your code. But it doesn't smell well.

V
Vadim ✓ Link copied!

I think, there's should be a balance of code minimization and code readability. Otherwise there might be an attribute that uses helper, that uses job, that uses action, that uses service, etc.

V
Vadim ✓ Link copied!

Povilas thank you for interesting topics and explanations. But now there is a question: What's the difference between Helper and Action? (from your videos I understud that it's the same things)

PK
Povilas Korop ✓ Link copied!

No, those are different things. Action is one method, usually related to some Eloquent model, like ModelUpdateAction or something. Helper can be one method about ANYTHING: format date/strings, change currency, etc.

V
Vadim ✓ Link copied!

I thought you can put anything into action, not only Eloquent stuff. In this video ( https://laraveldaily.com/lesson/structure-laravel-projects/service-or-action-class ) @3:57 You said that: Service is around some kind of a Model - to create, update, delete and all of that (of that Model), and Action is just a ONE action related to whatever. So, it still a bit unclear. Can you please clarify that? Thank you. (Would be nice to have line breaks preserved in the comments. They make text more readable. And also code blocks - as was mentioned in earlier comments.)

PK
Povilas Korop ✓ Link copied!

I think you are looking for some kind of a strict rule where there are NO RULES, that's what I wanted to tell in all this course. Both in actions and helpers you can place whatever you want, it's just about how I've seen people TYPICALLY use those.

As for comments, adding to the to-do list.

E
ex3m ✓ Link copied!

What about like Actions, we could use Transformers/Normalizers classes?

PK
Povilas Korop ✓ Link copied!

Yes, you could probably, but without specific examples it's hard to comment.

MS
Md Shahinur Rahman ✓ Link copied!

Provilas I am kind of basic in Laravel Development, Can you share your thought on, "What is difference between creating a helpers.php file , loading bunch of basic function that can be used on application and blade both side or making XYZClass and static method to do the same".

PK
Povilas Korop ✓ Link copied!

I've just published a video that may be relevant as an answer: Unsure Where to Put "Global" Method? Here's an Example.

LR
Luca Reghellin ✓ Link copied!

So I made a App\Helpers\AssetHelpers class. But if I use App\Helpers\AssetHelpers; in AppServiceProvider and then try to use it in the boot() method, I get a Undefined constant "App\Providers\AssetHelpers". So I don't understand: why don't you get the same error? What am I doing wrong?

M
Modestas ✓ Link copied!

Please show the code you are using as that is important. Somehow you are using it as a constant and not as a class

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.