Answer the question
In order to leave comments, you need to log in
How to properly build Laravel architecture?
I create a form for sending SMS. One page with an input and a button where the user enters his phone number and an SMS should come to him. At the moment I have 2 controllers and 2 services (not a service provider, but just a class that I called a service). I store logic in services and call functions from services through controllers.
The problem is that the request from the form comes to the MainController where new data is generated and written to the database along with the number. But there is also an SmsController that should send sms to a number. This number only comes in MainController. I see several solutions but I don't like them.
SmsController takes information from the database (There is an eloquent Connection class that does all the work with the database. ConnectionService creates an instance from Connection), but it’s not clear which number to pull out
Make 1 single controller where to shove both services / or also make only 1 service. But I think that 1 service per controller is considered correct (or is it not?)
Call a method from one controller to another.
Create a trait in which there will be functions, and implement them with services
Answer the question
In order to leave comments, you need to log in
In the service for creating an entity (for good, call it UseCase. Services are about interacting with the infrastructure), throw the PhoneAddedEvent event, and send SMS in its listener. It is possible synchronously, it is possible through queues.
A separate controller for sending SMS is not needed.
If we abstract from the implementation of the SMS provider, then create a service SmsSenderService implements SmsSenderInterface. To send SMS, in the PhoneAddedEvent event listener, use SmsSenderInterface.
Dispatch the new Job to the queue in MainConttolleer to send sms to the desired number
Form -> main controller -> model
View <- main controller <-^
Use setters and getters and you won't have to create 2 services. Your service is a model, it has business logic, there is no need to create a second service for your task. Why complicate when you can use setters and getters.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question