Comments (18)
@jeykey : the register will grow only if you use adapters for your classes, otherwise classical DI works fine. Also I have a trick for generating adapters for all actions classes which prevent it from growing, but it could be the subject of an entire article by itself.
@JeyKeu & @stvnyung : Actions are not Jobs or CQRS commands. They are meant to return data in a generic way which will be translated into a Response
. I think of them as the logical 'API' of an application. They can be a database Query as well. The important part is that they have to be Request/Response agnostic. I guess I should have insisted on this in the article :) Thanks for your feedback, really appreciated !
from blog-contest-may-mayhem.
@melokki : i'll ping you once i've written it :)
from blog-contest-may-mayhem.
@stvnyung ; from what I read Action would be closer to the Feature part in lucid architecture. Jobs are basically fire and forget; as @monaam pointed out, they cannot return values; they are designed for time-consuming task which you do not need to wait the response for completing a request, like sending an email, posting some data to a 3rd party API...
edit : after further reading, lucid's jobs are not really like laravel's jobs hence the confusion. I'd say that the main difference is by design is that Actions always have resolvable dependencies, and can be injected inside a controller/method, and lucid's jobs have their parameters in the constructor, so they cannot be injected
@monaam : Yes I often embed one/several actions inside a job, if I need a time consuming script to run async in the queue.
from blog-contest-may-mayhem.
@melokki @twocngdagz @orangeShadow : Decorating Action classes using generated proxies
from blog-contest-may-mayhem.
Thank you !
I usually group them by model/related group of models :
app/Actions/Users
app/Actions/Orders
...
Though in biggest projects, I tend to make an Actions
subfolder by logical "module" of the application :
/app/Cart/Actions
/app/Users/Actions
Does not really matter I guess as long as it make sense with the project you're working with and you keep things consistant ;)
from blog-contest-may-mayhem.
@JeyKeu I personally use synchronous Job for this, which avoid this registering issue.
from blog-contest-may-mayhem.
@RemiCollin great article. How do you deal with retrieving models. Do you create an action for a simple User::where('active', 1)->orderBy('id', 'desc')->get()?
from blog-contest-may-mayhem.
Really nice article @RemiCollin . One question I have, what folder structure do you use to store these action classes?
from blog-contest-may-mayhem.
Great, thank you very much :)
from blog-contest-may-mayhem.
Interesting, my question is as your action classes are growing, so does the the register
method of your AppServiceProvider
. It would be nice if you could discuss that as well. And by the way I don't buy returning value on command class.
from blog-contest-may-mayhem.
Also I have a trick for generating adapters for all actions classes which prevent it from growing, but it could be the subject of an entire article by itself.
Where I can read this article? :)
from blog-contest-may-mayhem.
@RemiCollin I get the point ! What is the benefits of creating a Action instead of a sync Job then ? I feel like both could do the same thing, are they really different or just the same in the end ?
P.S.: The idea of using Job like this mainly come from there https://github.com/lucid-architecture/laravel
from blog-contest-may-mayhem.
@RemiCollin can sync jobs return objects/values? I think a job logically will contain bunch of actions.
from blog-contest-may-mayhem.
@RemiCollin ping me too, great article 👍
from blog-contest-may-mayhem.
@RemiCollin and me! 👍
from blog-contest-may-mayhem.
Great article, I think what are you doing is somewhat ( ADR pattern ), but you don't separate the response in a new class ( the ADR way ).
In your way, how you deal with ( API Resources ) and where you put them ? inside actions ? or in the controller ?
from blog-contest-may-mayhem.
I know it's an older post. @RemiCollin this is great article, before running into it, I used similar approach (but with Service providers), but the problem I ran into is dealing with nested eager loading. Maybe you can shed some light onto it:
Let's take your example:
User (1-M)> Blogs (1-M)> Comments > (1-M)> User
Let's say I need to load user with latest blog posts and 5 top comments with comment's user. Do I eager load in my Action/User/LoadUser.php
User::with['blog','blog.comments', 'blog.comments.user'])...
But let's say I don't need to load comments with their users for some sections of my website.
Should I create: Action/User/LoadUserWithComments.php ?
Sometimes nested eagerloading can get pretty hectic (Especially for reporting) Or do I create a separate actions for each situation? Seems to be a lot of redundant code. Especially if business logic comes at play. (For example, only certain UserType(s) can be loaded or only logged in users can see comments. etc).
Maybe I am misunderstanding this?
from blog-contest-may-mayhem.
Hi, @RemiCollin
I have the same question with @michaelkove, should we create a new action class for a simple action such as getting data from a table?
And how to deal with more complex queries that come with some different sections? Such as some controllers need A & B action, but some controllers else also need C & D (both A, B, C, D are just simple actions). Should I create AActionClass, BActionClass, CActionClass and DActionClass?
This issue was not active for a long time, I glad to hear from you, sir!
from blog-contest-may-mayhem.
Related Issues (20)
- Logging model changes and 11 other ways to improve your Laravel projects HOT 1
- Avoid Race condition using lockForUpdate function and MySQL transactions.
- Realtime Job Management With Laravel and Vue HOT 1
- File upload with Laravel and VueJS HOT 6
- Cloaking field names to add more layer of security in your form.
- Masking IDs in URLs using hashids in Laravel HOT 2
- Ordering with JSON in Laravel
- Encrypting Your Model Attributes
- Encrypting your Model Attributes HOT 4
- Laravel Innovations: Making your own webhook mechanism through notifications HOT 2
- Introduction to Laravel… for an Eight-Year-Old
- The Magical Use of Macroables in Laravel
- Dynamic Eloquent Relation By Macro
- Getting Started With Laravel — The Lift Off
- The Good, the Bad and the Ugly of seeding data in production HOT 5
- Solving common public key problems in Laravel Forge (a.k.a. Forgetastic pubkeys and where to find them)
- Differentiate Laravel collections methods
- Laravel Continuous Integration & Delivery with Bitbucket Pipelines
- Extending an Open Source Tool with Laravel Spark
- Domain Driven Design in Laravel
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog-contest-may-mayhem.