📜 ⬆️ ⬇️

Simple Rest Api framework based on Phalcon

Hello to all.
Here I want to offer a small example of the implementation of a simple rest api based on the popular framemover Phalcon . Even fatkovically not implementation, but its framework. Immediately clarify that the article is designed for beginners. People designated and experienced are unlikely to be interested in the content.
Phalcon provides ample opportunities for development, as well as a very large freedom for creativity and the creation of good products, because the example is really very simple.

In fact, all that is needed is to create a specific action in the controller, the purpose of which is to make a return of the necessary data, while the routing is based on annotations to a specific action, for example:
<?php namespace RestApi\Api\Controllers; class IndexController extends RestController { /** * @Get("/get") */ public function getAction() { return ['getAction']; } /** * @Post("/post") */ public function postAction() { $this->setStatusCode(201); return ['postAction']; } /** * @Put("/put") */ public function putAction() { $this->setStatusCode(201); return ['putAction']; } /** * @Delete("/delete") */ public function deleteAction() { return ['deleteAction']; } } 

What does it mean: URI / get is available for GET requests, / post for Post and so on. The response returns a json representation of the value that the controller returns. I’ll clarify that api supports only json output format, at the same time it’s very easy to add a new one only by extending the RestController :: prepareResponse method.
Routing is built on annotations and supports a variety of formats that can be found here .
When creating a new controller, it is necessary to inherit it from the base RestController and add a description to /config/ruotes.php:
 $router->addModuleResource("api", "RestApi\Api\Controllers\NewController"); 

That's all!
Sources are here . Thank you very much for your attention and please do not judge strictly. Even if the implementation is not useful to anyone, perhaps the article will arouse interest in the framework itself, but it is really cool.

')

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


All Articles