Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time

绀轰緥

涓嬮潰灏嗛氳繃缂栧啓涓涓畝鍗曠殑鍥句功绠$悊绯荤粺鎺ュ彛锛屾紨绀 PhpBoot 鐨勪娇鐢ㄣ傚畬鏁寸殑绀轰緥鍙湪杩欓噷涓嬭浇銆

1. 鍏充簬 RESTful

褰撳墠 RESTful 宸茬粡涓嶆槸鏂伴矞鐨勫悕璇嶄簡锛屾姏寮鎶借薄鐨勫畾涔夛紝鎴戣涓轰竴涓氫織鐨勮В閲婂彲浠ユ槸锛氭寜鏂囦欢绯荤粺鐨勬柟寮忓幓璁捐鎺ュ彛锛屽嵆鎶婃帴鍙f彁渚涚殑鍔熻兘锛屽綋鍋氭槸瀵光滅洰褰曗濈殑鈥滄搷浣溾濄傛瘮濡備竴涓櫥褰曟帴鍙o紝鎸 RESTful 璁捐锛屽彲浠ユ槸POST /tokens/锛屽嵆鎶婄櫥褰曪紝褰撳仛鏂板缓涓涓护鐗岋紝杩欓噷鐨/tokens/灏辨槸鈥滅洰褰曗濓紝POST灏辨槸瀵圭洰褰曠殑鈥滄搷浣溾濄傚叧浜 RESTful 姣旇緝鍑嗙‘鐨勫畾涔夛紝鍙互鐪杩欓噷銆傚叧浜 RESTful 鏈浣冲疄璺碉紝鍙互鐪杩欓噷

2. 绀轰緥鎺ュ彛

涓嬮潰鎴戝皢婕旂ず濡備綍鐢 PhpBoot 缂栧啓涓缁勨滃浘涔︾鐞嗏濇帴鍙o紝杩欎簺鎺ュ彛鍖呮嫭锛

鎺ュ彛鍚 METHOD URI 璇锋眰绀轰緥 鍝嶅簲绀轰緥
鏌ヨ鍥句功 GET /books/ GET /books/?name=php&offset=0&limit=1
{
"total": 0,
"data": [
    {
        "id": 0,
        "name": "string",
        "brief": "string",
        "pictures": [
            "string"
        ]
    }
]
}
鑾峰彇鍥句功璇︽儏 GET /books/{id} GET /books/1
{
    "id": 0,
    "name": "string",
    "brief": "string",
    "pictures": [
        "string"
    ]
}
鏂板缓鍥句功 POST /books/ POST /books/

{
"id": 0,
"name": "string",
"brief": "string",
"pictures": [
"string"
]
}
123
鍒犻櫎鍥句功 DELETE /books/{id} DELETE /books/1

3. 椤圭洰鐩綍缁撴瀯

  • app
    • Controllers
      • Books.php 鎺ュ彛瀹炵幇
    • Entities
      • Book.php 鏁版嵁瀹炰綋瀹氫箟
  • config
    • config.php 閰嶇疆
  • public
    • index.php 鍏ュ彛
  • vendor 渚濊禆鍖

4. 鍏ュ彛

index.php 浣滀负椤圭洰鍏ュ彛锛 閫氬父鍙渶瑕佹寚瀹氶厤缃枃浠跺拰 Controllers 鐩綍鐨勮矾寰勫嵆鍙傛渶缁堥」鐩澶栨彁渚涚殑鎺ュ彛锛 鐢变笉鍚岀殑 Controllers 鐨勫疄鐜扮被鎻愪緵銆

<?php
require __DIR__.'/../vendor/autoload.php';

use PhpBoot\Application;

$app = Application::createByDefault(__DIR__.'/../config/config.php');
//鎵弿 Controllers 鐩綍锛岃嚜鍔ㄥ姞杞芥墍鏈夎矾鐢
$app->loadRoutesFromPath( __DIR__.'/../App/Controllers', 'App\\Controllers');
//鎵ц璇锋眰
$app->dispatch();

5. 鎺ュ彛瀹炵幇

5.1. 瀹氫箟 Book 瀹炰綋

涓轰簡鍦ㄤ笉鍚屾帴鍙i棿鍏变韩鈥滃浘涔︿俊鎭濈殑鏁版嵁缁撴瀯锛屾垜浠畾涔変竴涓疄浣撳涓嬶細

class Book
{
    /**
     * @var int
     */
    public $id;
    /**
     * 涔﹀悕
     * @var string
     */
    public $name='';

    /**
     * 绠浠
     * @var string
     */
    public $brief='';

    /**
     * 鍥剧墖url
     * @var string[]
     */
    public $pictures=[];
}

5.2. 瀹氫箟 Controller

杩欓噷瀹氫箟浜 Books 绫讳綔涓 Controller锛屽悗缁帴鍙e皢瀹炵幇鍦ㄦ Books 绫讳腑銆

/**
 * 鎻愪緵鍥句功绠$悊鎺ュ彛
 * @path /books/
 */
class Books
{

}

涓婅堪浠g爜涓殑娉ㄩ噴@path /books/ 琛ㄧず Books 涓嬫墍鏈夋帴鍙o紝閮戒娇鐢/books/ 浣滀负鍓嶇紑銆

5.3. 鏌ヨ鍥句功鎺ュ彛

/**
 * 鏌ヨ鍥句功
 *
 * @route GET /
 *
 * @param string $name  鏌ユ壘涔﹀悕
 * @param int $offset 缁撴灉闆嗗亸绉 {@v min:0}
 * @param int $limit 杩斿洖缁撴灉鏈澶ф潯鏁 {@v max:1000}
 * @param int $total 鎬绘潯鏁
 * @throws BadRequestHttpException 鍙傛暟閿欒
 * @return Book[] 鍥句功鍒楄〃
 */
public function findBooks($name, &$total, $offset=0, $limit=100)
{
    $total = 1;
    return [new Book()];
}

涓轰究浜庣悊瑙o紝杩欐浠g爜鍙槸杩斿洖浜嗕竴缁勫浐瀹氱殑鏁版嵁锛岀湡瀹炲満鏅笅搴旇杩樹細璁块棶鏁版嵁搴撴垨鑰呯紦瀛樸備笅闈㈠皢璇存槑杩欐浠g爜鐨勫伐浣滄柟寮忥細

  1. @route 瀹氫箟浜嗘鎺ュ彛鐨勮矾鐢憋紝鍗 GET /books/(鍔犱笂浜咢path 瀹氫箟鐨勫墠缂)銆

  2. @param 瀹氫箟浜嗘帴鍙g殑璇锋眰鍙傛暟鍜岀被鍨嬶紝濡傛灉涓嶅0鏄@param, 鎺ュ彛鍙傛暟灏嗕粠鍑芥暟瀹氫箟涓彁鍙栵紝 濡傛灉鍑芥暟瀹氫箟涓病鏈夌敵鏄庡弬鏁扮被鍨嬶紝鍒欏弬鏁扮被鍨嬭璁や负鏄 mixed銆

  3. @v 瀹氫箟浜嗗弬鏁扮殑鍙栧艰寖鍥达紝鑻ヤ笉鎸囧畾锛屾鏋跺皢鍙細鏍¢獙璇锋眰涓殑鍙傛暟绫诲瀷锛 鍗冲鏋滃弬鏁版槸绫诲瀷鏄 int锛屽垯璇锋眰涓弬鏁板繀椤绘槸鍙互杞崲鎴 int 鐨勭被鍨嬶紝濡 123 鎴栬"123"绛夛紝鍚﹀垯浼氳繑鍥 400 閿欒

  4. 鍑芥暟鐨勮繑鍥炲煎皢琚 json_encode 鍚庤緭鍑哄埌 body銆傚鏋滃嚱鏁扮殑鍙傛暟涓病鏈夊紩鐢ㄧ被鍨嬶紙寮曠敤绫诲瀷鐨勫弬鏁拌璁や负鏄緭鍑猴紝鑰屼笉鏄緭鍏ワ級锛宺eturn 鍦 json_encode 鍚庡嵆琚綋鍋 body 杈撳嚭锛屽惁鍒 return 灏嗚璧嬪肩粰 body 涓殑 "data"銆

  5. &$total 鏄紩鐢ㄧ被鍨嬬殑鍙傛暟锛屼細琚渶涓鸿緭鍑猴紝榛樿杈撳嚭鍒 body 涓悓鍚嶅彉閲忎腑銆傚杩欎釜鎺ュ彛涓紝鏈缁堣緭鍑虹殑 body 褰㈠紡濡備笅锛

    {
        "total": 1, //鏉ヨ嚜 &$total
        "data": [   //鏉ヨ嚜 return
            {
                "id": 0,
                "name": "string",
                "brief": "string",
                "pictures": [
                    "string"
                ]
            }
        ]
    }
  6. 濡傛灉甯屾湜灏 return 杈撳嚭鍒板叾浠栦綅缃紝鎴栬呬笉浣跨敤榛樿鐨勮緭鍏ュ弬鏁扮粦瀹氭柟寮忥紝鍙互浣跨敤@bind, 姣斿 @return Book[] 鍥句功鍒楄〃 {@bind response.content.books} 灏嗕娇 return 缁撴灉缁戝畾鍦 json 鐨 "books" 涓婏紝鑰屼笉鏄粯璁ょ殑 "data"銆

  7. $offset=0, $limit=100瀹氫箟浜嗛粯璁ゅ硷紝濡傛灉璇锋眰涓笉鍖呭惈杩欎簺鍙傛暟锛屽皢浣跨敤榛樿鍊笺

  8. 娉ㄩ噴 @return Book[]@throws BadRequestHttpException 骞朵笉浼氬鎺ュ彛鐨勮繑鍥炴湁浠讳綍褰卞搷锛 浣嗘槸浼氬奖鍝嶆枃妗h緭鍑哄拰杩滅▼璋冪敤锛圧PC锛夈

5.4. 鑾峰彇鍥句功璇︽儏鎺ュ彛

/**
 * 鑾峰彇鍥句功
 * @route GET /{id}
 *
 * @param string $id 鎸囧畾鍥句功缂栧彿
 *
 * @throws NotFoundHttpException 鍥句功涓嶅瓨鍦
 *
 * @return Book 鍥句功淇℃伅
 */
public function getBook($id)
{
    return new Book();
}

璺敱 @route GET /{id} 鎸囧畾浜 url 鐨 path 鏈変竴涓彉閲{id}锛屽彉閲忕殑鍊煎彲浠ラ氳繃鍑芥暟鍙傛暟 $id 鑾峰彇

5.5. 鏂板缓鍥句功

/**
 * 鏂板缓鍥句功
 *
 * @route POST /
 * @param Book $book {@bind request.request} 杩欓噷灏唒ost鐨勫唴瀹圭粦瀹氬埌 book 鍙傛暟涓
 * @throws BadRequestHttpException
 * @return string 杩斿洖鏂板缓鍥句功鐨勭紪鍙
 */
public function createBook(Book $book)
{
    return '123';
}
  1. 璇锋眰涓殑 json 浼氳妗嗘灦鑷姩杞崲鎴愬嚱鏁颁腑闇瑕佺殑瀵硅薄鍙傛暟銆
  2. {@bind request.request} 琛ㄧず鐢ㄨ姹傜殑 body 鏋勯 $book 鍙橀噺锛岃嫢涓嶆寚瀹欯bind锛岄粯璁ゆ槸鎻愬彇璇锋眰 body 涓 "book" 瀛楁鏋勯 $book 鍙橀噺锛屼篃灏辨槸璇磋姹備細鏄互涓嬪舰寮忥細
{
    "book": {
            "id": 0,
            "name": "string",
            "brief": "string",
            "pictures": [
                "string"
            ]
        }
}

5.6. 鍒犻櫎鍥句功

/**
 * 鍒犻櫎鍥句功
 *
 * @route DELETE /{id}
 * @param string $id
 * @throws NotFoundHttpException 鎸囧畾鍥句功涓嶅瓨鍦
 * @return void
 */
public function deleteBook($id)
{
    
}

濡傛灉鍑芥暟娌℃湁杩斿洖鍊硷紝鍒欏搷搴旂殑 http body 浼氭槸 void锛 鑰屼笉鏄┖瀛楃涓, 鍥犱负 鍩轰簬PhpBoot 瀹炵幇鐨勬帴鍙o紝榛樿鎯呭喌涓嬶紝http body 鎬绘槸 json锛岃岀┖瀛楃涓插苟涓嶆槸鍚堟硶鐨 json銆

6. 鏇村

鏇村鍐呭瑙: