Simple Rest Api Framework Based on Phalcon
Hello everybody.
So I want to offer a small example of the implementation of a simple rest api based on the popular Phalcon framework . Even fatally not an implementation, but its frame. I’ll immediately clarify that the article is designed for beginners. Recognized and experienced people are unlikely to be interested in the contents.
Phalcon provides great development opportunities, as well as very great freedom for creativity and creating good products, so 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 return the set of necessary data, while routing is based on annotations for a specific action, for example:
What it means: URI / get is available for GET requests, / post for Post, etc. The response returns a json representation of the value that the controller action will return. 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 expanding the RestController :: prepareResponse method.
Routing is built on annotations and supports a number of formats that can be found here .
When creating a new controller, it must be inherited from the base RestController and add a description to /config/ruotes.php:
That's all!
Sources are here . Thank you very much for your attention and please do not judge strictly. Even if implementation is not useful to anyone, perhaps the article will arouse interest in the framework itself, and it is really cool.
So I want to offer a small example of the implementation of a simple rest api based on the popular Phalcon framework . Even fatally not an implementation, but its frame. I’ll immediately clarify that the article is designed for beginners. Recognized and experienced people are unlikely to be interested in the contents.
Phalcon provides great development opportunities, as well as very great freedom for creativity and creating good products, so 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 return the set of necessary data, while routing is based on annotations for a specific action, for example:
setStatusCode(201);
return ['postAction'];
}
/**
* @Put("/put")
*/
public function putAction()
{
$this->setStatusCode(201);
return ['putAction'];
}
/**
* @Delete("/delete")
*/
public function deleteAction()
{
return ['deleteAction'];
}
}
What it means: URI / get is available for GET requests, / post for Post, etc. The response returns a json representation of the value that the controller action will return. 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 expanding the RestController :: prepareResponse method.
Routing is built on annotations and supports a number of formats that can be found here .
When creating a new controller, it must be inherited 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 implementation is not useful to anyone, perhaps the article will arouse interest in the framework itself, and it is really cool.