Laravel Controller Code: Move to Model, Service, Action or Job?

Laravel Controller Code: Move to Model, Service, Action or Job?

Laravel Daily

2 года назад

103,672 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Todd Horst
Todd Horst - 19.09.2023 00:31

Can someone explain why the code shouldn't live in the controller? Coming from other languages, that code is in the controller and as is stated the model is the structure of the class and it's relation to the DB.

Ответить
Mahmoud Noufal
Mahmoud Noufal - 29.08.2023 03:38

Thank you so much,

Ответить
James Kimani
James Kimani - 14.08.2023 01:22

Oops .... Ive been puting them in a form request class😂... Not even a honourable mention 😅

Ответить
Gustavo
Gustavo - 03.08.2023 03:22

I think its generally considered bad practice to use exceptions to handle validation errors. Exceptions should be reserved for unexpected errors that should not be handled by the application. I'd change error validations from exceptions to a MessageBag instance in the service class, and then catch the error by controller using MessageBag->errors

Ответить
Samuel E. Tedesque
Samuel E. Tedesque - 30.07.2023 06:22

Thanks a lot Povilas, I was trying to understand a way to separate the logic from my model Class to a Service class and your vídeo really helped me to get it. Thks for sharing

Ответить
ریحان ارشد
ریحان ارشد - 15.07.2023 16:32

If we follow the SOLID principle then we know that ‘S’ stands for Single Responsibility which means that a class should only be responsible for what it is for. It should never be responsible for any other related actions.

In this example: Order class should only be responsible for cRUD functionality of orders.
It shouldn’t have logic for creating an invoice or sending notifications or anything else.

For these tasks, having service classes is a better solution.

Again, those who have worked with Spring (Java) knows that you can have Service class which then calls Repository class for database related logic.

Off-course you can go further to DAO pattern. So from Repository you call the DAO class which then is used for database related logic.

Ответить
Guilherme dos Santos Souza Júnior
Guilherme dos Santos Souza Júnior - 18.05.2023 03:39

I wish every english speaker talked like you, I can perfectly understand every word!

Ответить
Miguel Radaza
Miguel Radaza - 29.04.2023 17:42

what tools you use for documentation of apis in laravel, can we use swagger in laravel?

Ответить
Aksoom Hussain
Aksoom Hussain - 05.04.2023 10:35

why don't consider observer instead of service for status update or sending notification?

Ответить
Michael O'Connor
Michael O'Connor - 27.03.2023 05:03

Great video. I know I'm late to the party, but I think throwing and catching Exception isn't such a great idea. Exception is so broad, you may end up catching a completely unrelated exception. I think it's better to throw and catch a more specific exception, like 'InvalidArgumentException' or 'RuntimeException'.

Ответить
Wade Zimmerman
Wade Zimmerman - 25.03.2023 22:59

I agree with this video however I never use actions. IMO if you want to reuse the same logic in a command, controller, job, etc., then the service should provide commands, controllers, and jobs. It keeps the design close to the framework and it makes your services reusable across projects. With actions, you're instantiating a lot of single use classes which are tightly coupled to your project.

For example, if I have an action called ProcessUserPaymentMethod, it seems like a concise action. However, as soon as the requirements change to do something like process a team payment method, or to process additional payment methods like wire transfers, now what? Do I continuing writing new action classes for each case, do I add methods to the action class, or do I rewrite what I have into a service?

My rule of thumb is keep models strictly for data relationships, and model configuration like you said. Use controllers to query the db, delegate logic, and render views. Use services to do all the heavy processing and provide extensions to the framework.

Ответить
Jeremy van der Wegen
Jeremy van der Wegen - 22.02.2023 23:21

What about:
If you would have a Customer model, that extends CustomerService, and CustomerService extends Eloquent Model. That way you can call a method on the object, for instance $customer->redoVerificationProcess(). No need to put the Customer model in an argument of a service class and no need for auto resolving a service class inside the controller. Also, Customer model will still be tiny and readable for relationships, mutators, scopes etc.

Ответить
DigitalEagleDev
DigitalEagleDev - 22.02.2023 20:57

Very good explanation ✔️✔️

Ответить
adel chellabi
adel chellabi - 22.02.2023 01:59

What about
Controller => service => repository => model

Ответить
Olexa FPV
Olexa FPV - 26.12.2022 22:30

Thanks! Very useful

Ответить
Diego Melgar
Diego Melgar - 18.12.2022 01:44

Thank you very much for what you have said in this video. I am not an expert in DDD but in you personal opinion, I would like to know if there would be some other actions/services that must be disptached o executed from the order invoice, should they be called in the controller one after the other? or can they be called inside the service? which for me makes more sense that a service or services can be called insider other services in order to communicate between each other.

Nice video Povilas!

Ответить
Gustiawan Ouwawi
Gustiawan Ouwawi - 01.12.2022 12:47

validation should go to controller but the logic should go to service in my opinion

Ответить
Erstevn
Erstevn - 20.11.2022 18:51

Man, such a quality content. I just watch another video of using repository pattern for Laravel application and then I found this, great content brother

Ответить
Jorge Shimada Brn
Jorge Shimada Brn - 15.11.2022 12:05

Thanks for sharing this information, very good video. I agree with you in keeping the business rules into a service or action.
I couldn't understand one point: if you need to pass the object $orderService as a parameter to the function in the controller, how do you do this in the route or even in the blade side to send it?
Thanks in advance

Ответить
Ahsan Khan
Ahsan Khan - 20.10.2022 14:08

Tip: Use the Command pattern when you want to queue operations, schedule their execution, or execute them remotely.

Ответить
B&W
B&W - 02.10.2022 17:56

Laravel directory structure itself is far from optimal if you plan to make a big project.
when it's a big project which has a big business logic, I use the module structure like in Python frameworks i.e., Django.
This structure for me is the best for big projects, every module has its own views, controllers, models, jobs, events, actions, business logic ..., and you don't have travel back and forth between unlinked directories when you're working on a single module.
I think Laravel in the future should consider this structure and must be an option while setup to choose this structure if you prefer.

Ответить
Charles M
Charles M - 28.09.2022 00:09

Great video. Thanks Povilas.

Ответить
Melanie Carr
Melanie Carr - 10.09.2022 07:24

This is over my head. I wish I understood more what you are talking about, but it looks way advanced.

Ответить
The AI Evolution Hub
The AI Evolution Hub - 09.09.2022 06:00

In my opinion, Model should be thin and contains only database configs, accessor and mutators. All database related (for example CRUD) operations should be in the respected Model Repository. Service class will utilize the model repository and do either database operation or external API call.

Ответить
Pierce McGeough
Pierce McGeough - 06.09.2022 02:11

How would this work with a repository implementation and would it be good/bad?

Ответить
andy wong
andy wong - 29.08.2022 06:21

is it overkill to use event/listeners for this Povilas?

Ответить
wdog
wdog - 23.08.2022 16:37

Thanks!

Ответить
GoldenCoder coder
GoldenCoder coder - 15.08.2022 17:45

First question is not where its why? Why cant the code bee in the controller? Does it speed up the framework if we move it?

Ответить
Dev
Dev - 16.07.2022 11:12

Thanks very nice.

Ответить
Schmueles
Schmueles - 02.07.2022 15:34

If all methods' arguments of a service start with a specific class, that's when I decide that the methods should probably be within the class, otherwise it becomes somewhat of a helper class for which you could just use static classes or functions. I don't see a problem with having many methods in a class if they're all specific to the class. Talking about god classes, you'd probably wan't to take a look at Laravel's internals, they're full of them.

Ответить
Rihulfa Akbar
Rihulfa Akbar - 16.06.2022 13:22

I always doing my logic in controller, but after watching this video I'm going to refactoring my controllers. Thanks for the insight 👍

Ответить
Said Abdellatif Said Bakr
Said Abdellatif Said Bakr - 15.05.2022 15:16

Where is Trait?

Ответить
Jim
Jim - 11.05.2022 19:33

I have something observation regarding on using Job with dispatchSync which definitely decrease the execution time of process about 40% instead of puting my logic plainly on controller.

Ответить
Kira Yamato
Kira Yamato - 24.04.2022 23:45

In java or groovy, Services class is all about businesses layer and logic. Do the internal logic in service and call it in controller. Controller should not use complicated logic and sql queries, let the service handle it.

Ответить
Connor Abbas
Connor Abbas - 12.04.2022 16:07

Anyone know the IDE color theme?

Ответить
Ramin Sadeghnasab
Ramin Sadeghnasab - 25.03.2022 22:53

Your videos are great, I can't even miss one second of them. Thanks for such a high quality content.

Ответить
Adam
Adam - 22.03.2022 01:05

Using invoke in action class is cool, you don't have to remember mething name, action class behave as method itself

Ответить
Adarsh Chacko
Adarsh Chacko - 16.03.2022 08:21

Clicked on the LIKE button the moment he started talking about the topic because this was something that always troubled me. Looking forward to more of such videos.

Ответить
Swift
Swift - 20.02.2022 02:22

You are a fantastic teacher!

Is it too confident to say that just use service? If later on service becoming cumbersome which requires refactor, just separate them using action and job. So service will be only end point that a controller touches?

Ответить
Diego
Diego - 15.02.2022 16:03

Services and Actions arent the same then use Event and Listenners? i mean have they the same purpose?

Ответить
Amruth Charan
Amruth Charan - 11.02.2022 11:51

Why don't we just return the invoice number instead of whole object to the controller from action or service?

Ответить
Slimani Youcef
Slimani Youcef - 10.02.2022 21:27

Personally, if i go from Service to Action, it's for complexity reasons !
I mean, if you've got a Service class with over 1000 lines of code, then refactoring it into a set of Action classes is not a bad idea.

Ответить
iOS Haven
iOS Haven - 03.02.2022 09:48

I create a “packages” folder and create local service providers in that folder. Each service provider package is a git submodule and linked as a composer repository of type “path”. This allows me to go crazy on OOP and only publish what is necessary to my app. It also allows me to reuse services across projects.

Ответить
hooman mirghasemi
hooman mirghasemi - 21.01.2022 23:19

Thanks for your video. Your service class is like Reposetory Design pattern. So it is better to rename it and move to reposetory directory and also making interface and ....

Ответить
Alireza Salehi
Alireza Salehi - 21.01.2022 11:01

very useful channel

Ответить
Alireza Salehi
Alireza Salehi - 21.01.2022 11:01

very useful channel

Ответить
Derick Masai
Derick Masai - 21.01.2022 10:27

Here I am, having written an edtech startup's 6 months worth of code on controllers 😅 Buuut they don't call it MVP for nothing. Make it work, then optimize it as you go 😂

Ответить
Gene Artista
Gene Artista - 21.01.2022 01:48

I would rather use your method of creating a Services directory, it seems more readable and easier to manage than the other two, Do you have a tutorial about PEST?

Ответить