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 Answers3

0

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

suyanlu
  • 109
  • 1
  • 1
  • 4
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

Tom St
  • 908
  • 8
  • 15
  • 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" – Todor Atanasov Feb 10 '18 at 16:38
  • 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 https://stackoverflow.com/questions/13453746/php-front-controller-with-mvc – Tom St Feb 10 '18 at 16:49
0

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

This requires prior knowledge on using Composer

Raaghu
  • 1,956
  • 1
  • 19
  • 17