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

璺敱

PhpBoot 鏀寔涓ょ褰㈠紡鐨勮矾鐢卞畾涔夛紝 鍒嗗埆鏄氳繃鍔犺浇 Controller 绫伙紝鍒嗘瀽 Annotation 锛岃嚜鍔ㄥ姞杞借矾鐢憋紝鍜岄氳繃 Application::addRoute 鏂规硶鎵嬪姩娣诲姞璺敱銆

1. 鑷姩鍔犺浇璺敱

浣犲彲浠ラ氳繃 Application::loadRoutesFromClass 鎴栬 Application::loadRoutesFromPath 娣诲姞璺敱銆傛鏋舵壂鎻忔瘡涓被鐨勬瘡涓柟娉曪紝濡傛灉鏂规硶鏍囪浜咢route锛屽皢琚嚜鍔ㄦ坊鍔犱负璺敱銆傝鍔犺浇绫荤殑褰㈠紡濡備笅锛

/**
 * @path /books
 */
class Books
{
    /**
     * @route GET /{id}
     */
    public function getBook($id)
}

浠ヤ笂浠g爜琛ㄧず http 璇锋眰 GET /books/{id} 鍏跺疄鐜颁负 Books::getBook, 鍏朵腑{id}涓簎rl 鐨勫彲鍙橀儴鍒嗐

1.1. @path

璇硶锛 @path <prefix>

鏍囨敞鍦ㄧ被鐨勬敞閲婇噷锛岀敤浜庢寚瀹 Controller 绫讳腑鎵瀹氫箟鐨勫叏閮ㄦ帴鍙g殑uri 鐨勫墠缂銆

1.2. @route

璇硶锛 @path <method> <uri>

鏍囨敞鍦ㄦ柟娉曠殑娉ㄩ噴閲岋紝鐢ㄤ簬鎸囧畾鎺ュ彛鐨勮矾鐢便俶ethod涓烘寚瀹氱殑 http 鏂规硶锛屽彲浠ユ槸 GET銆丠EAD銆丳OST銆丳UT銆丳ATCH銆丏ELETE銆丱PTION銆丏ELETE銆倁ri 涓彲浠ュ甫鍙橀噺锛岀敤{}鍖呭洿銆

2. 鎵嬪姩鍔犺浇璺敱

浣犲彲浠ヤ娇鐢 Application::addRoute 鎵嬪姩鍔犺浇璺敱锛屾柟娉曞涓嬶細

$app->addRoute('GET', '/books/{id}', function(Request $request){
    $books = new Books();
    return $books->getBook($request->get('id'));
});

闇瑕佹敞鎰忕殑鏄紝姝ゆ柟娉曟坊鍔犵殑璺敱锛屽皢涓嶈兘鑷姩鐢熸垚鎺ュ彛鏂囨。銆