📜 ⬆️ ⬇️

REST API for a couple of minutes with Lionframe

Code generators simplify the life of an ordinary developer; in any case, they are called upon to do this, saving him from the boring writing of the same type of code. One of these generators team Sylius'a presented as a gift for the community for Christmas.


Foreword


A couple of weeks ago, while being interviewed, I received a test task of writing a simple REST API in pure PHP and MySQL. It took me about four hours to encode. Having stumbled upon the mysterious @ Lakion 's tweet about the rapid creation of the REST API, I became interested and decided to check out this new animal. Here are a couple of words about the results.

API generation


The shortest way to achieve our goal is described in the tutorial and requires only three steps from us:
  1. Generation of the entity that will be the resource of our service.
  2. Configuration SyliusResourceBundle'a.
  3. Configuration routing.

Such shamanism will lead to the creation of five familiar CRUD methods: index (GET method), show (GET), create (POST), update (PUT / PATCH), delete (DELETE). Now let's try to understand what is happening under the hood.
')
So, we gave the creation and mapping of the entity to the generator for Doctrine, which created a beautiful entity with access methods for the properties and metadata we described in the format we chose. The basis of all the magic is the SyliusResourceBundle, the configuration of which allows you to add user resources in this way:
sylius_resource: resources: my_app.entity_key: driver: doctrine/orm # required manager: default templates: App:User classes: model: MyApp\Entity\EntityName # required interface: MyApp\Entity\EntityKeyInterface controller: Sylius\Bundle\ResourceBundle\Controller\ResourceController repository: Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository 

Only two parameters are required: driver and classes.model. If we do not define a custom controller, then ResourceController will be used, kindly provided to us by Sylius.

Another interesting moment in Lionframe is routing. Tutorial offers us to configure it like this:
 acme_artist: resource: acme.artist type: sylius.api 

For these two parameters to become five routes, the ApiLoader is used, and specifically its load method, which creates all five routes for each resource. When creating a URL, a plural form of the resource name is used (in this case, artists). The meaning of the type parameter is that the routing system can distinguish these specific routes from the usual ones and, accordingly, for the first ones to pull the ApiLoader.

Results


Creating your own wooden REST API with your own hands - a few hours, with Lionframe - a few minutes. QoS confirms that code generators are good. I would also like to ask the habreators how often all sorts of code generators are used in real commercial projects and how much of the generated code lives at least until the middle of the project?

Links


Source: https://habr.com/ru/post/246827/


All Articles