Answer the question
In order to leave comments, you need to log in
Is it the right approach in Laravel?
I had a bold controller, which was responsible for downloading new information from the API of 6 different services. Each service has its own method.
I created an App\Services folder where I created 6 classes to work with each service. Each class has one method.
Then I initialized these classes in the controller constructor
public function __construct(FirstApi $firstApi,
SecondApi $secondApi,
ThirdApi $thirdApi)
{
$this->firstApi= $firstApi;
$this->secondApi= $secondApi;
$this->thirdApi= $thirdApi;
}
public function getInfoFromFirstApi()
{
$this->firstApi->getInfo();
}
Answer the question
In order to leave comments, you need to log in
Correctly. Business logic should be in services.
However, if your services are registered with the provider, which you seem to have done, then they can be injected directly into the desired controller methods.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question