1

I want to build a REST API using PHP, but without any framework. By the following requirments:

  • The code should be as simple as possible with OOP development principles in mind, easy to read and expand
  • Data should be kept in MySQL and to be returned as JSON in the given format
  • DO NOT use ANY Framework or ANY already written code, but to have structure
  • User input data validation
  • There should be no security issues

At first, I thought I should build complete MVC project, but I realized that actually I will probably don't need any views and I will use Services instead of controllers. And also models for both entities (Articles and Users).

I'm still not sure what is the perfect way to do it, so I will just tell you what I`m thinking so far...Sorry if Its a duplicate post but I haven't found much information about this and from the little I found, I got more confused.

I thinking of a simple router.php class that will have a method:

map($httpMethod, $route, $callback)

So, for example, I will call ("POST", "/users/register", registerUser(params)) or ("GET", "/users", registerUser(params)), just like I would do in a MVC web app.

I think I will need a model and a service for each of both entities. The service will execute the SQL for each CRUD operation. I think I know how to create the service, as it won't be much different than a controller.

But I wonder how can I create the model part for both entities. What exactly I will need for the models as a code?

First of all, it would be nice if you agree that this is the right way and if not, I would love to hear a lot of criticism because I'm currently confused and really don't know where to start.

3 Answers 3

0

I agree with you. And suggest you to know about Loopback, it's good, like what you described.

Sign up to request clarification or add additional context in comments.

3 Comments

I know that, and you should learn loopback how to do this, it is just like what you want.
What does it exactly do? Can you edit the answer to explain a bit?
I have updated the post with the requirements that I have to face
0

I know your question is how to build your own rest api without framework, but it sounds like you'd actually make good use of at least some components (not necessarily a whole framework), do you really need/want to write a router from beginning?

if so sure, if no maybe some microframework? anyway, symfony has some info on how you would create your own framework (just as an example), they use couple of their own classes (i.e. httpcomponent), but just for the explanation of idea/way how things you want are done.

https://symfony.com/doc/current/create_framework/index.html

2 Comments

I have updated the post with the requirements that I have to face. Do you think what you propose will be counted as a "written code"
Ok, I see... so this is an example of how it os done in a nice framework, they use some existing classes Request, Response, Routing, but just have a look at them and you'll get idea of what you need to do. If it doesnt suit your needs then have a good read of this one stackoverflow.com/questions/13453746/…
0

I found this library in PHP for get started with REST API's php-platform/restful

This requires prior knowledge on using Composer

1 Comment

Do you have any example of implementations?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.