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

TarikAli's avatar

Share logic between api and front

Hello, i have a project front,admin and api . i want to know where to put the logic to be shared between the three parts . i will not use repository . i mean like functions that check or return data and so in api controller return it as json and in others return it with view.

also in api should i use resource .

Thanks

0 likes
2 replies
drewdan's avatar
drewdan
Best Answer
Level 15

In our projects, we use a "service layer" - basically any shared business logic gets put into \App\Services\WhateverService.php.

That way we can use this service in multiple controllers to prevent code being duplicated.

If, for example, a controller was called InteractionController, we would name the service InteractionService and inject it into the controller, and any other controller it was required in.

Please or to participate in this conversation.