Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cancreate's avatar

Seprate Service Class for API & Web?

I'm working on an eCommerce project where I've a service class ShipperService.php. Inside that service class I've got 2 methods authenticate() & register(). Now shipper can only authenticate via API call the response of which will be in JSON but, when it comes to register() method the shipper can register via API call as well as admin can create an account for shipper the response of which can be JSON or a simple boolean response.

Now my question is should I use separate service classes for APIs and Web routes or should I write some logic and make one method that can be used by both API controllers and Web controllers?

0 likes
2 replies
Sinnbeck's avatar

I would use an interface to force that both methods are added and then use 2 classes that implement that interface

martinbean's avatar

Now my question is should I use separate service classes for APIs and Web routes or should I write some logic and make one method that can be used by both API controllers and Web controllers?

@cancreate No. The entire point of abstracting logic services classes is so it can be reused. Creating service classes per “context” completely defeats the point of them.

Please or to participate in this conversation.