如何使用PHP編寫RESTful介面

來源:互聯網
上載者:User

PHPRS @github

這是一個輕量級架構,專為快速開發RESTful介面而設計。如果你和我一樣,厭倦了使用傳統的MVC架構編寫微服務或者前後端分離的API介面,受不了為了一個簡單介面而做的很多多餘的coding(和CTRL-C/CTRL-V),那麼,你肯定會喜歡這個架構!

先舉個栗子

  1. 寫個HelloWorld.php,放到架構指定的目錄下(預設是和index.php同級的apis/目錄)

    /** * @path("/hw") */class HelloWorld{    /**      * @route({"GET","/"})     */    public function doSomething() {        return "Hello World!";    }}
  2. 瀏覽器輸入 http://your-domain/hw/

    你將看到: Hello World!就是這麼簡單,不需要額外配置,不需要繼承也不需要組合。

發生了什麼

回過頭看HelloWorld.php,特殊的地方在於注釋(@path,@route),沒錯,架構 通過注釋擷取路由資訊和綁定輸入輸出。但不要擔心效能,注釋只會在類檔案修改後解析一次。更多的@注釋後面會說明。

再看個更具體的例子

這是一個登入介面的例子

/** * 使用者權限驗證 * @path("/tokens/")  */class Tokens{     /**     * 登入     * 通過使用者名稱密碼授權     * @route({"POST","/accounts/"})      * @param({"account", "$._POST.account"}) 帳號     * @param({"password", "$._POST.password"}) 密碼     *      * @throws ({"InvalidPassword", "res", "403 Forbidden", {"error":"InvalidPassword"} }) 使用者名稱或密碼無效     *      * @return({"body"})         * 返回token,同cookie中的token相同,     * {"token":"xxx", "uid" = "xxx"}     *     * @return({"cookie","token","$token","+365 days","/"})  通過cookie返回token     * @return({"cookie","uid","$uid","+365 days","/"})  通過cookie返回uid     */    public function createTokenByAccounts($account, $password, &$token,&$uid){        //驗證使用者        $uid = $this->users->verifyPassword($account, $password);        Verify::isTrue($uid, new InvalidPassword($account));        $token = ...;        return ['token'=>$token, 'uid'=>$uid];    }     /**     * @property({"default":"@Users"})   依賴的屬性,由架構注入     * @var Users     */    public $users;}

還能做什麼

  1. 依賴管理(依賴注入),

  2. 自動輸出介面文檔(不是doxgen式的類、方法文檔,而是描述http介面的文檔)

  3. 介面緩衝

  4. hook

配合 ezsql訪問資料庫

ezsql是一款簡單的物件導向的sql構建工具,提供簡單的基本sql操作。介面

/** @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);   }    /**     * 依賴注入PDO執行個體     * @property     * @var PDO     */    public $db;}

設定檔

{    {        "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"                   }    },}

手冊

請移步 github

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.