r/PHPhelp • u/AngelSlash • Dec 11 '24
Solved Creating a REST API
Hello everyone
As the title says I'm trying to create a REST API. For context, I'm currently creating a website (a cooking website) to learn how to use PHP. The website will allow users to login / sign in, to create and read recipes. After creating the front (HTML, CSS) and the back (SQL queries) I'm now diving in the process of creating my API to allow users to access my website from mobile and PC. (For context I'm working on WAMP).
The thing is I'm having a really hard time understanding how to create an API. I understand it's basically just SQL queries you encode / decode in JSON (correct me if I'm wrong) but I don't understand how to set it up. From what I've gathered you're supposed to create your index.php and your endpoints before creating the HTML ? How do you "link" the various PHP pages (for exemple I've got a UserPage.php) with the endpoints ?
Sorry if my question is a bit confusing, the whole architecture of an API IS still confusing to me even after doing a lot of research about it. Thanks to anyone who could give me an explaination.
View all comments
Show parent comment
1
u/equilni Dec 12 '24 edited Dec 12 '24
I was using library code to help illustrate the point. I noted I agree with you, but it needed to be clarified better.
Also the abstraction part could be argued where this may not matter as much, which I did.
Eloquent ORM would be similar (though I know this not used in this manner)
I don't recall where this was noted...
Also, Laravel's database component (Query Builder & ORM) can be used outside of the framework
My preference would be a DTO (as well as SQL) if we are talking vanilla, so
$postStore->insert($postDTO);. DTOs can also be used elsewhere in the codebase.Consider:
At the end, we are saying similar things, just different ways to go about it.