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.
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.
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)
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.
I thought you can put anything into action, not only Eloquent stuff.
In this video ( //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.)
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.
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".
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?
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.
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.
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)
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.
I thought you can put anything into action, not only Eloquent stuff. In this video ( //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.)
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.
What about like Actions, we could use Transformers/Normalizers classes?
Yes, you could probably, but without specific examples it's hard to comment.
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 makingXYZClass
and static method to do the same".I've just published a video that may be relevant as an answer: Unsure Where to Put "Global" Method? Here's an Example.
So I made a
App\Helpers\AssetHelpers
class. But if Iuse App\Helpers\AssetHelpers;
in AppServiceProvider and then try to use it in the boot() method, I get aUndefined constant "App\Providers\AssetHelpers"
. So I don't understand: why don't you get the same error? What am I doing wrong?Please show the code you are using as that is important. Somehow you are using it as a constant and not as a class