Good Practice in APIS Rest Full with PHP and Doctrine ORM 2

Asked

Viewed 239 times

1

Currently we have a system that uses Doctrine ORM2, we have our Entities, and themes our DAOS, within our DAOS classes we have several methods that we use to make queries, Change, Insert and Delete Data in the Database.

We have created some prefixes for these operations:

  • slt => select

  • lst => list list

  • get => usually search for a specific ID

  • alt => change

  • ins => insert

  • del => delete

Thus a name for a method in the class User that will insert a new user or seek a specific in the system would be:

insUsuario or getUsuario

So far so good, the problem is that I am creating an api and wanted to use these DAOS methods already ready to do various operations in the bank, I am using the Slim Framework to help me create the api, and I wanted to use the DAOS as follows:

https://api.com/usuarios/getUsuario/5

https://api.com/usuarios/getUsuario?id=5

Where:

  • Where users => Users
  • Where getUsuario => User class method

But I researched and read that using method names in an api is not good practice, and I have classes here that have up to 50 different methods, and I don’t know what to do because I wanted to use these ready-made methods, how could I do this using my methods that are already ready in the DAOS ?

  • 1

    What are the advantages of using the right HTTP methods?, https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design e http://www.restapitutorial.com/lessons/restquicktips.html

  • 1

    The idea of REST is to represent a resource so using verbs or actions in Uri names is not recommended. It was not clear in the question you use some mechanism that hits the controller and home with the method name and executes it? The name of your Daos' methods does not need to go to the Union.

  • When making a request using this url for example: https://api.com/usuarios/getUsuario/5, I treat the name "usuarios", transform into Usuariosdao, instantiate this DAO class using Reflectionclass and check if the getUsuario method exists in this class, if there are arguments for the method and return the result of the query.

  • You said that the name of the Daos methods need not go to url, but how will I know, or manage what the user really wants ?

  • 1

    A route will point to the desired method the verb can also help in this case. No need to outsource all methods from one class to the API, only return what is useful.

  • I understood then a whole route should reference only one specific method ? And as the API grows I create the routes and also the methods ?

  • It is not a route by method, the user does not need to know its class structure. The important point is to define the purpose from the API, who will use it? which features/services are interesting to my consumers? which features will I outsource? I forgot to comment on, the site of the restapitutorial has a pdf to donwload recommend reading the sections Querying, Filtering and Pagination and Resource Naming Anti-Patterns

  • I get it, I’m gonna take a look here and I’m gonna follow what you said.

Show 3 more comments

1 answer

0


I used the manual that friend rray indicated to me, and I went very deep into the theory which made me have a greater understanding of how this technology works to why apply the best methods of programming for the resolution of my problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.