PHP writing a RESTful interface method, PHP writing restful_php tutorial

Source: Internet
Author: User

PHP writes a restful interface method, and PHP writes restful


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.
1, write a helloworld.php, put in the framework specified directory (by default, and index.php sibling apis/directory)

/** * @path ("/HW") */class helloworld{  /**    * @route ({"GET", "/"}) */public  function dosomething () {    return "Hello world!";  }}

2. 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 "          }  },}

The above is the whole content of this article, I hope that everyone's study has helped.

Articles you may be interested in:

    • PHP routing class sharing for restful requests
    • PHP implementation automatically identifies the return content type of the RESTful API
    • PHP makes cross-platform Restfule interface based on Curl extension

http://www.bkjia.com/PHPjc/1101653.html www.bkjia.com true http://www.bkjia.com/PHPjc/1101653.html techarticle PHP Writing a RESTful interface method, PHP writing restful This is a lightweight framework designed for fast development of restful interfaces. If you're like me, tired of using the traditional MVC framework ...

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