×ðÁú¿­Ê±

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ÔõôдµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿