First, let's meet the restful.
RESTful is a design style rather than a standard, such as an interface originally: HTTP://WWW.TEST.COM/USER/VIEW/ID/1
Represents getting user information with ID 1, which can be changed if you use restful style:
Http://www.test.com/user/1
The benefits of doing this can be clearly seen:
1, more concise URL, programmer-friendly
2, do not expose the internal code structure, more secure
So, how do you implement this interface? First, we need to receive the/USER/1 section.
$path = $_server[' path_info '); $arr = explode ('/', $path); Print_r ($arr);
Get the following result:
Array ([0] = [1] = User [2] = 1)
Get the parameters, the following is a simple operation:
if ($arr [1] = = ' user ') {$model = new Usermodel (); $id = $arr [2];//read user information $user _info = $model->find ($id); Echo Json_enco De ($user _info); }
In this way, we have implemented a restful style of API.
Below, we look at how to implement the interface to read the user list, the traditional way: http://www.test.com/user/list
Using restful styles, you can be more concise:
Http://www.test.com/user
And the difference between reading user information is that there is no ID after user, so we can modify it on the basis of the Reading section:
if ($arr [1] = = ' user ') {$model = new Usermodel (); $id = $arr [2]; if ($id) {//Read user information $user _info = $model->find ($id); echo Json_encode ($user _info); }else{//Read user list $user _list = $model->select (); Echo Json_encode ($user _list);}}
This enables the interface to read the list of users.
Let's look at how to implement an additional user interface in the traditional way:
Http://www.test.com/user/add
RESTful style:
Http://www.test.com/user
and read the user list interface is the same, how to distinguish it? In fact, it is very simple, the read is a GET request, and the increase is a POST request, the user information is present in the post parameters, so you can make the following changes to the code:
The same interface allows different logic to be executed according to the HTTP request method.
This is the core idea of implementing the RESTful style API, and so on, we can implement the edit user interface and delete the user interface:
Http://www.test.com/user/1
As long as you use different request methods, you can distinguish them. We can use the put request to perform the edit operation, and delete the deletion request.
The specific code is not written, interested friends can try to achieve the following.
Support for restful styles is also provided in the thinkphp framework, which is similar in principle.
To summarize, the restful style is to implement the interface using a simple URL and HTTP request method.
Phprs GitHub
This is a lightweight framework designed for fast development of restful interfaces. If you are like me, tired of using the traditional MVC framework to write a microservices or a front-end decoupled API interface, can not stand for a simple interface to do a lot of redundant coding (and ctrl-c/ctrl-v), then, you will love this framework!
Give me a chestnut first.
Write a helloworld.php that is placed in the directory specified by the framework (default is the apis/directory with index.php siblings)
/** * @path ("/HW") */class helloworld{ /** * @route ({"GET", "/"}) */public function dosomething () { return "Hello world!"; }}
Browser input http://your-domain/hw/
You will see: Hello world! is so simple, no additional configuration is required, no inheritance is required, and no combination is required.
What happened
Looking back at helloworld.php, the special place is the annotation (@path, @route), yes, the framework obtains the routing information and the binding input and output through annotations. But don't worry about performance, comments will only parse once after the class file has been modified. More @ comments are explained later.
Let's look at a more specific example.
This is an example of a login interface
/** * User Rights verification * @path ("/tokens/") */class tokens{ /** * Login * Authorized by Username password * @route ({"POST", "/accounts/ "}) * @param ({" Account "," $._post.account "}) accounts * @param ({" Password "," $._post.password "}) password * * @ Throws ({"Invalidpassword", "res", "403 Forbidden", {"error": "Invalidpassword"}}) the username or password is invalid * * @return ({" Body "}) * Return token, same as token in cookie, * {" token ":" XXX "," uid "=" XXX "} * * @return ({" Cookie "," Token "," $token "," +365 Days ","/"}) return token * @return via Cookie ({" Cookie "," UID "," $uid "," +365 Days ","/"}) Return UID via cookie * /Public Function createtokenbyaccounts ($account, $password, & $token,& $uid) { //Verify user $uid = $this->users->verifypassword ($account, $password); Verify::istrue ($uid, New Invalidpassword ($account)); $token = ...; return [' token ' = $token, ' uid ' = ' $uid]; } /** * @property ({"Default": "@Users"}) dependent properties, injected by framework * @var Users */public $users;}
What else can I do?
- Dependency management (Dependency injection),
- Auto-output interface documentation (not Doxgen class, method document, but document describing HTTP interface)
- Interface Cache
- Hook
Accessing the database with Ezsql
Ezsql is a simple object-oriented SQL build tool that provides simple basic SQL operations.
Interface
/** @path (/myclass) */class myclass{ /** * @route ({"GET", "/do"}) * @param ({"arg0", "$._get.arg0"}) * /Public dosomething ($arg 0) { return sql::select (' xxx ')->from (' table_xxx ')->where (' xxx =? ', $arg 0)- >get ($this->db); } /** * Dependency Injection PDO Instance * @property * @var PDO */public $db;}
Configuration file
{ " MyClass": {" properties": { "db": "@db1"} }, }, "DB1": { " Singleton ": True, " class ":" PDO ", " pass_by_construct ": True, " Properties ": { " DSN ":" Mysql:host =127.0.0.1;dbname=xxx ", " username ":" xxxx ", " passwd ":" xxxx " } },}
PHP RESTful interface