How to use PHP to compile the RESTful interface PHPRS @ github
This is a lightweight framework designed for rapid development of RESTful interfaces. If you are just like me, get tired of using the traditional MVC framework to write microservices or APIs separated from the frontend and backend, can't stand the lots of extra coding (and CTRL-C/CTRL-V) done for a simple interface, then you'll definitely love this frame!
Let's start with a chestnut.
-
Write HelloWorld. php and put it in the directory specified by the framework (the default is the apis/directory at the same level as index. php)
/** * @path("/hw") */class HelloWorld{ /** * @route({"GET","/"}) */ public function doSomething() { return "Hello World!"; }}
-
Enter http: // your-domain/hw/in the browser/
You will see: Hello World! It is so simple that no additional configuration is required, no inheritance or combination is required.
What happened
Looking back at HelloWorld. php, the special thing is that the annotation (@ path, @ route ).Obtain route information and bind input and output through annotations. But don't worry about performance. the comment will only be parsed once after the class file is modified. More @ comments will be described later.
Let's look at a more specific example.
This is an example of a logon interface.
/*** User permission verification * @ path ("/tokens /") */class Tokens {/*** logon * authorization by username and password * @ route ({"POST", "/accounts/"}) * @ param ({"account ", "$. _ POST. account "}) account * @ param ({" password "," $. _ POST. password "}) password ** @ throws ({" InvalidPassword "," res "," 403 Forbidden ", {" error ":" InvalidPassword "}}) invalid username or password ** @ return ({"body"}) * returns the token, which is the same as the token in the cookie. * {"token": "xxx ", "uid" = "xxx"} ** @ return ({"cookie", "token", "$ token", "+ 365 days ","/"}) return token * @ return ({"cookie", "uid", "$ uid", "+ 365 days", "/"}) through cookie ","/"}) return uid */public function createTokenByAccounts ($ account, $ password, & $ token, & $ uid) through cookie) {// Verify the user $ uid = $ this-> users-> verifyPassword ($ account, $ password); Verify: isTrue ($ uid, new InvalidPassword ($ account )); $ token = ...; return ['token' => $ token, 'uid' => $ uid];}/*** @ property ({"default": "@ Users "}) dependency attribute, injected by the framework * @ var Users */public $ users ;}What else can I do?
-
Dependency Management (dependency injection ),
-
Automatic output interface document (not a doxgen-type class or method document, but a document describing the http interface)
-
Interface cache
-
Hook
Access 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 ($ arg0) {return SQL: select ('XXX')-> from ('Table _ XXX ') -> where ('XXX =? ', $ Arg0)-> 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" } },}Manual
Please move to github