V
V
VZVZ2016-02-27 22:26:03
PHP
VZVZ, 2016-02-27 22:26:03

What is the best way to make a REST API architecture that takes the same content from the MySQL database and returns it to the client in different formats: JSON, XML and HTML?

Currently there is 1 script:
images.get.json.php
at root/api/images.get
Planned to have 3 scripts:
images.get.json.php
at root/api/images.get
images.get.xml.php
at to root/api/images.get.xml
images.get.html.php
to root/api/images.get.html ("Return responses as HTML" sounds strange, but if you look closely at
some VK or Facebook, then it’s the same with them, only they don’t apply to the API and differ from it beyond recognition.And I want this not only to facilitate AJAX, but also for use in systems like Cordova / Sciter / NW.js and other things, where UI is built on HTML+CSS(+JS))
The subconscious mind says: well, don’t make 3 separate scripts, make 1 common one, with a parameter (json | xml | html), and then let if ... else if ... else parse it, but give the URL the proper look through htaccess ..
But alas .
I hate htaccess, I hate regular expressions, this is a natural Brainfuck, I don’t like too cumbersome scripts either, in short, I want separate files, period.
And since they are separate, it is necessary, obviously, to somehow separate the very receipt of data from the database (this is a mysql result) from those 3 scripts that are only busy presenting data. And connect everything.
Of course, direct access to the model from the client is prohibited.
What is the best way to do this?
If I did it for myself, I would not bother, I would stupidly include the model in each of the 3 scripts.
But, my code will be evaluated by Serious Western Seniors, so I think, maybe it’s better otherwise, maybe there is some generally accepted solution like a pattern?
It would be desirable without MVC frameworks (this is already a specific project).
Although MVC would somehow simplify things a little ... The idea is clear: 1 Model, 3 Action / View, but it looks clumsy ... Or not?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Krasavin, 2016-02-27
@okwinza

If you are going to do a normal REST, then give it in the format that the user requests in the `Accept` header, for exampleAccept: text/plain

I
Igor, 2016-02-27
@unitby

Still, one script is better. Which makes the same request, creating the same object. well, the model.
or if you want 3 scripts, then include this one. and then bring the object to mind what you want to give. in different scripts it turns out in its own way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question