I have multi endpoint for my website ( Backend, Api, internal api for application and ... )

There is a lot of code that duplicate between this endpoints.

for example for signup I repeat these lines 3 times

$user = User::create(['email'=>input('email')])->save();
// and there is more logic

So my question is how to prevent duplicate some business logic? where to put these shared logic in MVC?

As I know in MVC controller is for business logic, but how Can I share my codes between them?

I have some shared logic between my endpoint and also each end point has individual logic too.

these business logic is about application behavior (not validating and other stuff that relative to model).

  • You have a received several downvotes and this is likely because you have asked a question about the "best way" of doing things. There is no universally best way of doing things so your question is likely to result in people providing different opinions which is something that this site attempts to avoid. You should ask more specific questions that can be answered in a more specific manner. – Jason K. Jun 17 at 15:37
  • @JasonK.I'm looking for way that I stuck in it, the problem is simple, Where to put shared business logic in MVC and with multi endpoint. I think the downvote because they don't unserstand my problem. I'm stuck in this problem and this comminuty is for situation like me. – Ali Akbar Azizi Jun 17 at 17:25
  • @JasonK.I updated my question, I appreciate that you look at it. – Ali Akbar Azizi Jun 17 at 17:50
  • 1
    The controller's responsibility is definitely not business logic. It's responsibility is coordinating a user's request. The controller delegates control to the classes that execute business logic. – Greg Burghardt Jun 17 at 18:51
  • @GregBurghardt ok, I know that, but the problem still exist. where to put this logic? – Ali Akbar Azizi Jun 18 at 5:33

For 2 lines? In separate applications that may very well drift apart?

Probably not worth the effort.

Now lets say you have an actual complex piece of logic, or logic that must be kept identical between applications.

Why not use a shared library?

  • Each application can use this one implementation (even if at different versions)
  • Later if you want to turn this into a networked service, you already have an API that can be used to implement a proxy, and be used as the basis for the network interface.
| improve this answer | |
  • 2 line is an example. I know I need a shared code between application, My question is how to implement it? with which design pattern? – Ali Akbar Azizi Jun 17 at 11:12
  • Don't over think it. If code is shared place it in a function/object. If those are shared between code bases wrap it in a library. If the data has to be owned by the library, wrap it in a service. – Kain0_0 Jun 18 at 0:34
  • I don't unserstand this part If the data has to be owned by the library, wrap it in a service. can you explain it? – Ali Akbar Azizi Jun 18 at 5:36
  • Take the example of user login you gave. The Library can have all of the mechanics of checking a data source for a user, validating their password, performing some other spurious checks such as logging in from a know IP/Machine/Browser etc... But the application that is using the library has to supply the data that is being used by the library. – Kain0_0 Jun 18 at 6:25
  • ... If that data is not owned by the application, and is in fact shared between all of these applications, what you have is a case where the Library itself owns the data. In this case the Library and the data should be move to its own application space and present a login service to these applications. – Kain0_0 Jun 18 at 6:25

If the only difference between your signup enpoints is their name (e.g. /api/v1/signup vs /backend/signup, etc) and otherwise the same request and response structures can be used, then you should try to arrange it such that the routing logic points both endpoints to the same Controller method.

If that doesn't work, then the MVC pattern is a good way to keep the code duplication as low as possible.

The main requirement of the MVC pattern is that you can identify 3 parts of the code with the following responsibilities:

  • Controllers: These receive the requests
  • Views: These transform the model information into a format consumable by the client/user
  • Model: The rest of the software, including the business logic and everything that would be common for multiple (groups of) Controllers.

As the MVC Model can be huge, it is very normal that it is further subdivided in various ways. Having a Service Layer is a normal part of such a sub-division.

| improve this answer | |
  • of course there is some core specific for each endpoint and there is some share code between them. so I can't use same controller for all endpoint right? Thanks for information about service layer. but I'm still confused, Is service layer good for this sitatuon – Ali Akbar Azizi Jun 17 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.