PHP Method for compiling RESTful interfaces _ php instances

Source: Internet
Author: User
This article describes in detail how to compile a RESTful API in PHP. If you are interested, refer to this 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). That's right. The framework obtains the route information and binds the input and output through the annotation. 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"          }  },}

The above is all the content of this article, hoping to help you learn.

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.