O
O
Orbb2018-06-14 13:53:42
Yii
Orbb, 2018-06-14 13:53:42

How to implement a web interface + api for an application in yii2?

Hello!
Please tell me the right direction in the implementation of the following functionality:
there is, in general, a regular goods accounting application, it has its own controllers, modules, models, everything is as it should be. And then you need to implement an API for it for various purposes.
At first I easily imagined it, but now I'm completely confused.
I have several options: a separate api controller + pull up the modules I need. Because all the main functionality is already ready, I will duplicate it in the api controller, calling the necessary methods. Cons: double code, Pros: independent of the main implementation.
Or write your own module, which will already interact internally with other modules and controllers. The downside here is that when updating one model, you will need to make changes in two places. The advantages are the same, only all this is wrapped neatly in a module.
Maybe I missed something or I don't know. Tell me how to implement all this correctly and not get selected spaghetti at the output :)
ps links are welcome

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2018-06-14
@Maksclub

all the main functionality is already ready, I will duplicate it in the api controller, calling the necessary methods

That's why everyone says "thin controllers"
Since you have logic in the controllers, then yes - duplicate
If they were smashed normally by services - then the controllers on the back and api would simply use them and were essentially consumables

M
Maxim Timofeev, 2018-06-14
@webinar

we take an advanced template, api is a separate app, its own config, its own access control, response in the form of json, etc.
the code should not be duplicated if everything is done correctly. As Maxim Fedorov said , a model should be thick, then you can reuse it without problems in your usual web controllers and rest controllers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question