Recently, we need to use PHP to provide backend APIs for mobile apps. We can see that there are few documents related to PHPRestfulAPI in China. If you want to learn a piece of code, PHP is only a few days old, now you need to know how to complete this API elegantly. Recently, we need to use PHP to provide backend APIs for mobile apps. We can see that there are few documents related to PHP Restful APIs in China. To learn a code, we can only get started with PHP for a few days, now you need to know how to complete this API elegantly.
Reply content:
Recently, we need to use PHP to provide backend APIs for mobile apps. We can see that there are few documents related to PHP Restful APIs in China. To learn a code, we can only get started with PHP for a few days, now you need to know how to complete this API elegantly.
If you meet the following two conditions:
-
I am not very familiar with PHP;
-
Only a few simple APIs are required;
-
Project time is relatively tight;
So, I suggest you choose a simple small RESTful framework.
Why:
-
If you are not familiar with PHP, it is difficult to grasp a huge framework (such as Phalcon, Laravel, YII, and so on) at once );
-
For general developers, it is important to pay homework on time;
-
Using PHP to implement RESTful APIs, the most important thing is: Implementation of routing, resolution of HTTP requests, good encapsulation of HTTP response, and good support of JSON format;
If so, I will recommend it to you:
-
Slim
-
Bullet
More lightweight small frameworks search for REST by themselves.
However, it cannot be said that since we want to learn from other people's code, it should be better with the purpose and question. We recommend that you first look at the excellent design of some RESTful APIs, such as GitHub, Mailgun, and Enchant.
In addition, you can also refer to some "best practices" on the network, which is a lot more and will not be listed here. (Check GitHub at a Glance)
After having some knowledge of REST, I think it will get twice the result with my thoughts.
Finally, if you need a full-stack PHP framework (which takes some time to grasp) to implement REST APIs, you can also choose, such as YII and Laravel. But I still like Phalcon most. It is also a simple tutorial to implement REST.
Non-Flarum does not belong to and is completely separated from the frontend and backend.
Frontend and backend communication are based on jsonapi.
Reference
We recommend a framework phprs suitable for RESTful and microservice interfaces.
Let you get rid of MVC in these scenarios.
Let's take a look at the example. This is a possible login interface implementation. You only need to write the following code. The additional inheritance and configuration are invalid:
/*** User authentication ** @ path ("/tokens/") */class Tokens {/*** login * @ route ({"POST ", "/accounts/"}) * @ param ({"account", "$. _ POST. account "}) account * @ param ({" password "," $. _ POST. password "}) the password * @ return ({" body "}) also returns the token through the body, which is the same as the token in the cookie, {" token ": "xxx"} * @ return ({"cookie", "token", "$ token", "+ 365 days ","/"}) return token */public function createTokenByAccounts ($ account, $ password, & $ token) through cookie {// verify the user $ token = ...; return ['Token' => $ token];}
The Framework defines routing and binding parameters through @ annotation. In addition, there are some useful advanced features: dependency injection, automated interface documentation, interface caching, and so on.
Although this framework looks quite new on github, it is actually very stable and can be used in online environments.