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

鍙傛暟缁戝畾

瀹炵幇鎺ュ彛鏃讹紝閫氬父闇瑕佷粠 http 璇锋眰涓彁鍙栨暟鎹紝浣滀负鏂规硶鐨勮緭鍏ュ弬鏁帮紝骞跺皢鏂规硶鐨勮繑鍥炲艰浆鎹㈡垚 http 鐨勮緭鍑恒傚弬鏁扮粦瀹氬姛鑳藉嵆鍙互甯綘瀹屾垚涓婅堪宸ヤ綔銆

1. 杈撳叆缁戝畾

1.1. 鏍规嵁鏂规硶瀹氫箟缁戝畾

榛樿鎯呭喌涓嬶紝妗嗘灦浼氫粠http璇锋眰涓彁鍙栧拰鏂规硶鐨勫弬鏁板悕鍚屽悕鐨勫彉閲忥紝浣滀负鍑芥暟鐨勫弬鏁般傛瘮濡傦細

/**
 * @route GET /books/
 */
public function getBooks($offsit, $limit)

涓婅堪浠g爜锛屽搴旂殑 http 璇锋眰褰㈠紡涓 GET /books/?offsit=0&limit=10銆傚湪姝ら粯璁よ姹備笅锛

  • 濡傛灉璺敱 uri 涓畾涔変簡鍙橀噺锛屽弬鏁板皢浼樺厛閫夊彇 uri 鍙橀噺銆傚锛
/**
 * @route GET /books/{id}
 */
public function getBook($id)

鍏朵腑 $id 鍙栬嚜 uri銆

  • 瀵逛簬娌℃湁 BODY 鐨 http 璇锋眰锛圙ET銆丠EAD銆丱PTION銆丏ELETE锛夛紝鍙傛暟鏉ヨ嚜 querystring 銆

  • 鍏朵粬璇锋眰锛圥OST銆丳UT銆丱PTION锛夛紝鍙傛暟鍏堝彇 querystring锛屽鏋滄病鏈夛紝鍐嶅彇 BODY銆

1.2. @param

濡傛灉鍦ㄦ柟娉曠殑娉ㄩ噴涓紝鏍囨敞浜 @param锛屽氨浼氭湁鐢 @param 鐨勭粦瀹氫俊鎭鐩栭粯璁ゆ潵鑷嚱鏁板畾涔夌殑缁戝畾淇℃伅銆侤param 鍙互鎸囧畾鍙橀噺鐨勭被鍨嬶紝鑰屽師鍑芥暟瀹氫箟涓彧鑳藉湪鍙傛暟鏄暟缁勬垨鑰呭璞℃椂鎵嶈兘鎸囧畾绫诲瀷銆侤param 鐨勮娉曚负鏍囧噯 PHP Document 鐨勮娉曘

/**
 * @route GET /books/
 * @param int $offsit
 * @param int $limit
 */
public function getBooks($offsit, $limit)

浠ヤ笂浠g爜锛岄櫎浜嗙粦瀹氬彉閲忓锛岃繕鎸囧畾浜嗗彉閲忕被鍨嬶紝鍗冲鏋滆緭鍏ュ兼棤娉曡浆鎹㈡垚 int锛屽皢杩斿洖 400 BadRequest 閿欒銆傛湭鎸囧畾@param 鏃讹紝鍙傛暟鐨勭被鍨嬮粯璁や负 mixed銆

1.3. 杈撳叆瀵硅薄鍙傛暟

杈撳叆鍙傛暟闄や簡鏄師鐢熺被鍨嬪锛岃繕鍙互鏄璞★紙杩欓噷鎴戜滑鎶婂彧鏈夊睘鎬у拰 get銆乻et 鏂规硶鐨勫璞★紝绉颁负瀹炰綋锛圗ntity锛夛級銆傚锛

/**
 * @route POST /books/
 * @param Book $book {@bind request.request} 灏$_POST 鍐呭杞崲鎴怋ook瀹炰緥
 */
public function createBook(Book $bok)

鍏朵腑 Book 鐨勭殑瀹氫箟锛

/**
 * 鍥句功淇℃伅
 */
class Book
{
    /**
     * @var int
     * @v optional
     */
    public $id;
    /**
     * 涔﹀悕
     * @var string
     */
    public $name='';

    /**
     * 绠浠
     * @var string
     * @v lengthMax:200
     */
    public $brief='';

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

妗嗘灦瀵 http 璇锋眰鍒板疄浣撶殑杞崲锛屾湁涓濂楄嚜宸辩殑閫昏緫锛

  • @var 鎸囧畾灞炴х殑绫诲瀷锛屽鏋滅被鍨嬩笉鍖归厤锛屽疄渚嬪寲灏嗘姏鍑 InvalidArgumentException 寮傚父
  • 濡傛灉涓嶆爣娉 @var锛屽垯榛樿绫诲瀷涓簃ixed
  • 濡傛灉灞炴ф湁榛樿鍊硷紝琛ㄧず姝ゅ睘鎬у彲閫夛紝鍚﹀垯璁や负姝ゅ睘鎬у繀閫
  • 鏀寔 @v 瀹氫箟鏍¢獙瑙勫垯
  • 瀹炰綋鍙互宓屽

1.4. 鍙傛暟榛樿鍊

濡傛灉鎯虫寚瀹氭煇涓緭鍏ュ弬鏁板彲閫夛紝鍙渶缁欐柟娉曞弬鏁拌缃竴涓粯璁ゅ笺傛瘮濡:

/**
 * @route GET /books/
 * @param int $offsit
 * @param int $limit
 */
public function getBooks($offsit=0, $limit=10)

娉ㄦ剰锛歱hp 鏂规硶鐨勯粯璁ゅ弬鏁, 蹇呴』鏀惧湪鏂规硶鐨勬渶鍚

2. 杈撳嚭缁戝畾

2.1. 缁戝畾return

榛樿鎯呭喌涓嬶紝鍑芥暟鐨勮繑鍥炲煎皢 jsonencode 鍚庯紝浣滀负 body 杈撳嚭銆傚

/**
 * @route GET /books/{id}
 */
public function getBook($id)
{
    return ['name'=>'PhpBook', 'desc'=>'PhpBook Document'];
}

curl 璇锋眰灏嗗緱鍒颁互涓嬬粨鏋

$ curl "http://localhost/books/1"
{
    "name": "PhpBook",
    "desc": "PhpBook Document"
}

娉ㄦ剰锛岃繖閲屼负渚夸簬婕旂ず锛岀洿鎺ュ湪鏂规硶涓繑鍥炰簡鏁扮粍锛堝叾瀹炶繖鍦ㄥ叾浠栬瑷閲岀畻瀵硅薄锛夛紝浣嗕綘搴旇涓鸿繖绉嶈繑鍥炲畾涔変竴涓被锛岄鍏堬紝鏈夊緢澶氭敼鍠勪唬鐮佽川閲忕殑鐞嗙敱榧撳姳浣跨敤瀵硅薄鏇夸唬杩欑被鏁扮粍锛屽叾娆″湪鑷姩鐢熸垚鏂囨。鏃讹紝杩欑被鏁扮粍鏃犲彂琚粨鏋勫寲鎻忚堪銆

2.2. 缁戝畾寮曠敤鍙傛暟

濡傛灉鏂规硶鐨勫弬鏁版槸寮曠敤绫诲瀷锛屽垯杩欎釜鍙傛暟灏嗕笉浼氫粠璇锋眰涓幏鍙栵紝鑰屾槸浣滀负杈撳嚭銆傛瘮濡傦細

/**
 * @route GET /books/
 * @param int $offsit
 * @param int $limit
 * @return Books[]
 */
public function getBooks($offsit=0, $limit=10, &$total)
{
    $total = 1;
    return [new Books()];
}

curl 璇锋眰灏嗗緱鍒颁互涓嬬粨鏋

$ curl "http://localhost/books"
{
    "total": 1,
    "data": [
        {
            "name":null, 
            "desc":null
        }
    ]
}

鍙互鐪嬪埌锛$total 杈撳嚭鍒颁簡 http body 涓 娉ㄦ剰锛氬綋鎺ュ彛瀛樺湪寮曠敤鍙傛暟鏃讹紝鎺ュ彛鐨勮繑鍥炲煎皢浼氳榛樿缁戝畾鍒皉esponse.content.data锛屾晥鏋滃拰澹版槑{@bind response.content.data}涓鑷淬

3. @bind

閫氳繃@bind锛屽彲浠ユ敼鍙橀粯璁ょ殑缁戝畾鍏崇郴锛屽皢鍙傛暟涓庡叾浠栬緭鍏ラ」缁戝畾锛屽锛

/**
 * @route GET /books/
 * @return Books[] {@bind response.content.books}
 */
public function getBooks($offsit=0, $limit=10, &$total)

琛ㄧず灏嗚繑鍥炵粦瀹氬埌鍝嶅簲 body 鐨 books 鍙橀噺锛堝搷搴旈粯璁ゆ槸 json锛夈

3.1. 缁戝畾杈撳叆

  • 璇锋眰Body: request.request
  • Query String: request.query
  • Cookie锛 request.cookies
  • **璇锋眰Header锛**request.headers
  • **鏂囦欢锛**request.files

3.2. 缁戝畾杈撳嚭

  • 鍝嶅簲Body: response.content
  • Cookie锛 response.cookies
  • **璇锋眰Header锛**response.headers