W
W
WebDev2016-08-15 11:14:12
Laravel
WebDev, 2016-08-15 11:14:12

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;
    }

And after that I call the service methods in the controller methods.
public function getInfoFromFirstApi()
{
    $this->firstApi->getInfo();
}

Did I understand the idea of ​​the "Service layer" pattern correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-08-15
@kirill-93

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 question

Ask a Question

731 491 924 answers to any question