How to use PHP to write RESTful interfaces

Source: Internet
Author: User
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.
  1. 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!";    }}
  2. 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?
  1. Dependency Management (dependency injection ),

  2. Automatic output interface document (not a doxgen-type class or method document, but a document describing the http interface)

  3. Interface cache

  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.