O
O
Ordec2019-09-06 12:57:12
PHP
Ordec, 2019-09-06 12:57:12

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?)

The code will be ugly and everything will be jumbled together.
Call a method from one controller to another.

Don't like it either. Since it looks wretched (I also read that this is not a very good way to organize the code).
Create a trait in which there will be functions, and implement them with services

The two services do not have common functions

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dpws, 2019-09-06
@Ordec

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.

A
Alexey Nikolaev, 2019-09-06
@Heian

Dispatch the new Job to the queue in MainConttolleer to send sms to the desired number

S
Shady1010, 2019-09-07
@Shady1010

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 question

Ask a Question

731 491 924 answers to any question