laravel apiÔõôд
Ëæ×Å»¥ÁªÍøÓ¦ÓúÍÊÖ»úÓ¦ÓõÄÉú³¤£¬Ðí¶àÆóÒµºÍ¿ª·¢Õß×îÏȽÓÄÉÇ°ºó¶ËÊèÉ¢µÄ¼Ü¹¹£¬ºó¶ËÌṩrestful api¸øÇ°¶ËʹÓã¬ÔõÑù¿ìËÙ¸ßЧµØ±àдapi³ÉΪÁËÐí¶à¿ª·¢Õß¹Ø×¢µÄÒ»¸öÎÊÌâ¡£laravelÊÇÒ»¿îÊ¢ÐеÄphp web¿ò¼Ü£¬ËüÌṩÁËÍêÉƵÄ·ÓÉ¡¢orm¡¢ÖÐÑë¼þµÈ×é¼þ£¬Ê¹µÃ±àдapi±äµÃÒì³£¼òÆÓ¡£±¾ÎĽ«Ì½ÌÖÔõÑùÔÚlaravel¿ò¼ÜÖбàдrestful api¡£
×°ÖúÍÉèÖÃLaravel¿ò¼Ü
Ê×ÏÈÐèҪװÖÃLaravel¿ò¼Ü£¬¿ÉÒÔͨ¹ýLaravelµÄ¹ÙÍø»òÕßcomposer¾ÙÐÐÏÂÔØ£º
composer create-project --prefer-dist laravel/laravel api
µÇ¼ºó¸´ÖÆ
½ÓמÙÐÐÉèÖ㬿ÉÒÔÔÚ.envÎļþÖоÙÐÐÊý¾Ý¿â¡¢»º´æ¡¢ÐÐÁеÈ×é¼þµÄÉèÖá£ÎªÁËÀû±ãÆð¼û£¬¿ÉÒÔÔÚ .envÎļþÖÐÉèÖÃÊý¾Ý¿âÐÅÏ¢£º
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=api DB_USERNAME=root DB_PASSWORD=
µÇ¼ºó¸´ÖÆ
²¢ÇÒÔÚconfig/database.phpÎļþÖÐÌí¼ÓÊý¾Ý¿âÐÅÏ¢£º
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => false, 'engine' => null, ],
µÇ¼ºó¸´ÖÆ
Éè¼ÆAPI½Ó¿Ú
ÔÚ±àдAPI֮ǰ£¬ÐèҪȷ¶¨APIµÄ¹¦Ð§ºÍËùÐèµÄÇëÇó²ÎÊý¼°ÏìÓ¦Êý¾Ý£¬ÕâÒ»²½ºÜÊÇÖ÷Òª£¬¾öÒéÁËAPIµÄʹÓ÷½·¨ºÍЧ¹û¡£¾Ù¸öÀý×Ó£¬¼ÙÉèÎÒÃÇÒª±àдһ¸ö»ñÈ¡ÉÌÆ·ÁбíµÄAPI£¬ÄÇôÎÒÃÇÐèÒªÒÔϼ¸¸öÐÅÏ¢£º
ÇëÇó·½·¨£ºGET
ÇëÇóURL£º/api/products
ÇëÇó²ÎÊý£ºÎÞÐè²ÎÊý
ÏìÓ¦Êý¾Ý£ºÉÌÆ·Áбí
ÏÂÃæÊÇÒ»¸öʾÀý£º
use AppProduct; Route::get('/api/products', function () { return Product::all(); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇʹÓÃLaravel¿ò¼Ü×Ô´øµÄ·ÓɺÍÄ£×Ó£¬Ê×ÏÈÔÚ·ÓÉÎļþÖнç˵ÁËÒ»¸öGETÇëÇóµÄURLµØµã/api/products£¬È»ºóÔÚ¶ÔÓ¦µÄ¿ØÖÆÆ÷ÒªÁìÖзµ»ØÁËËùÓеÄÉÌÆ·Êý¾Ý¡£ÕâÀïÎÒÃÇʹÓÃÁËORM£¨¹¤¾ß¹ØϵӳÉ䣩µÄ·½·¨²Ù×÷Êý¾Ý¿â£¬¿ÉÒÔ¿ìËÙÀû±ãµØ¾ÙÐÐÊý¾Ý²Ù×÷¡£
ÖÐÑë¼þµÄʹÓÃ
ÖÐÑë¼þÊÇLaravel¿ò¼ÜÖеÄÒ»¸öÖ÷Òª¿´·¨£¬Ëü¿ÉÒÔÔÚÇëÇóµÖ´ï¿ØÖÆÆ÷ÒªÁì֮ǰ»òÖ®ºóÖ´ÐÐһЩ²Ù×÷£¬ÀýÈçÉí·ÝÑéÖ¤µÈ¡£ÔÚ±àдAPIʱͨ³£ÐèÒªÌí¼ÓһЩÖÐÑë¼þÀ´È·±£APIµÄÇå¾²ÐÔºÍÐÔÄÜ¡£
¾Ù¸öÀý×Ó£¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒ»ÆäÖÐÑë¼þÀ´ÑéÖ¤ÇëÇóµÄÍ·²¿ÐÅÏ¢£¬ÀýÈçAPI°æ±¾¡¢TokenÊÇ·ñÕýµ±µÈ£¬È»ºóÔÚ¿ØÖÆÆ÷ÒªÁìÖоÙÐÐÊý¾ÝÅÌÎʺͷµ»Ø¡£Ê¾Àý´úÂëÈçÏ£º
use AppHttpMiddlewareVerifyApiToken; use AppHttpMiddlewareVerifyApiVersion; Route::middleware([VerifyApiToken::class, VerifyApiVersion::class])->group(function () { Route::get('/api/products', function () { return Product::all(); }); });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇͨ¹ýmiddlewareÒªÁìÌí¼ÓÁËÁ½ÆäÖÐÑë¼þVerifyApiTokenºÍVerifyApiVersion£¬ËüÃÇ»áÔÚÇëÇóµÖ´ï¿ØÖÆÆ÷ÒªÁì֮ǰִÐУ¬È·±£ÇëÇóµÄÇå¾²ÐԺͰ汾һÖÂÐÔ¡£
Òì³£´¦Àí
ÔÚ±àдAPIʱ£¬Òì³£´¦ÀíÒ²ÊÇÒ»¸öÖ÷ÒªµÄ»·½Ú£¬Ëü¿ÉÒÔÈÃÎÒÃǸüºÃµØ²¶»ñºÍ´¦ÀíÒì³£ÇéÐΣ¬ÀýÈçÊäÈë²ÎÊý¹ýʧ¡¢ÅÌÎÊÊý¾Ý²»±£´æµÈ¡£ÔÚLaravel¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýAppExceptionsHandlerÀàÀ´Í³Ò»´¦ÀíÒì³£ÇéÐΡ£
¾Ù¸öÀý×Ó£¬¼ÙÉèÎÒÃÇҪʵÏÖ»ñÈ¡µ¥¸öÉÌÆ·µÄAPI£¬²¢ÇÒÔÚÉÌÆ·²»±£´æʱ·µ»Ø404״̬ÂëºÍ¹ýʧÐÅÏ¢¡£´úÂëʾÀýÈçÏ£º
Route::get('/api/products/{id}', function ($id) { $product = Product::find($id); if (!$product) { abort(404, 'ÉÌÆ·²»±£´æ'); } return $product; });
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃÇͨ¹ýfindÒªÁìÅÌÎÊÉÌÆ·Êý¾Ý£¬ÈôÊÇÅÌÎÊЧ¹ûΪnull£¬ÔòÖ±½ÓʹÓÃabortÒªÁì·µ»Ø404״̬ÂëºÍ¹ýʧÐÅÏ¢¡£Í¨¹ýʹÓÃÒì³£´¦Àí£¬¿ÉÒÔÈÃÎÒÃǵÄAPIÔ½·¢½áʵºÍÎȹ̡£
×îºó
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÒѾ¿ÉÒÔ×îÏȱàд×Ô¼ºµÄRESTful APIÁË¡£Laravel¿ò¼ÜÌṩÁËÍêÉƵÄ·ÓÉ¡¢ÖÐÑë¼þ¡¢ORMµÈ×é¼þ£¬Ê¹µÃAPI±àдºÜÊǼòÆÓ¸ßЧ¡£ËäÈ»£¬¸üÏêϸ¡¢¸üרҵµÄAPI±àдָÄÏÉÐÓÐÐí¶à£¬Ï£Íû±¾ÎÄÄÜΪ¶ÁÕßÌṩ²Î¿¼ºÍÆôʾ¡£
ÒÔÉϾÍÊÇlaravel apiÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡