×ðÁú¿­Ê±¹ÙÍøµÇ¼

̽ÌÖLaravelÖÐÖÖÖÖÇëÇó·½·¨µÄʹÓúʹ¦Àí·½·¨

laravelÊÇÒ»¿îÔÚwebÓ¦ÓóÌÐò¿ª·¢ÖйãÊܽӴýµÄphp¿ò¼Ü¡£ËüÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§ºÍ¹¤¾ß£¬Ê¹µÃ¿ª·¢Ö°Ô±¿ÉÒÔÔ½·¢¸ßЧµØÍê³É³£¼ûµÄʹÃü¡£ÆäÖÐÒ»¸ö³£¼ûµÄʹÃü¾ÍÊÇ´¦ÀíhttpÇëÇó¡£laravelÖ§³Ö¶àÖÖ²î±ðµÄÇëÇó·½·¨£¬°üÀ¨get¡¢post¡¢put¡¢deleteµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«»á̽ÌÖlaravelÖÐÖÖÖÖÇëÇó·½·¨µÄʹÓúʹ¦Àí·½·¨¡£

HTTPÇëÇóºÍÏìÓ¦

ÔÚ×îÏÈÏÈÈÝÖÖÖÖÇëÇó·½·¨Ö®Ç°£¬ÈÃÎÒÃÇÏȼòÒªÏÈÈÝÒ»ÏÂHTTPÇëÇóºÍÏìÓ¦µÄ»ù±¾¿´·¨¡£HTTPÇëÇóÊÇÖ¸¿Í»§¶ËÏòЧÀÍÆ÷·¢Ë͵ÄÒ»·ÝÇëÇó£¬ÆäÖаüÀ¨ÇëÇóµÄÄ¿µÄURL¡¢ÇëÇóÍ·ÐÅÏ¢ºÍÇëÇóÌ壨¹ØÓÚPOSTÇëÇ󣩡£Ð§ÀÍÆ÷ÔÚÎüÊÕµ½ÇëÇóºó£¬»á¾ÙÐÐÏìÓ¦µÄ´¦Àí²Ù×÷£¬È»ºóÏò¿Í»§¶Ë·¢ËÍÒ»·ÝHTTPÏìÓ¦¡£ÏìÓ¦ÖаüÀ¨ÏìӦͷÐÅÏ¢¡¢ÏìÓ¦ÂëºÍÏìÓ¦Ìå¡£ÏìÓ¦Âëָʾ×ÅЧÀÍÆ÷¶ÔÇëÇóµÄ´¦ÀíЧ¹û£¬ÀýÈç200ÌåÏÖÀֳɣ¬404ÌåÏÖÕÒ²»µ½ÇëÇóµÄ×ÊÔ´µÈ¡£

GETÇëÇó

GETÇëÇóÊÇÓÃÓÚ´ÓЧÀÍÆ÷»ñÈ¡Êý¾ÝµÄÒ»ÖÖÇëÇó·½·¨¡£ËüµÄÇëÇó²ÎÊý»á¸½¼ÓÔÚURLºóÃ棬²¢ÒÔÎʺţ¨£¿£©×÷ΪÍÑÀë·û¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute::get()ÒªÁìÀ´½ç˵һ¸öGET·ÓÉ¡£ÀýÈ磺

Route::get('/users', function () {
    return view('users');
});

µÇ¼ºó¸´ÖÆ

Õâ¸ö·Óɽ«»áÆ¥Åä/users·¾¶£¬²¢·µ»ØÒ»¸öÃûΪusersµÄÊÓͼ¡£ÔÚÕâ¸öÊÓͼÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃһЩHTML±êÇ©À´ÌìÉúÒ»¸öGETÇëÇó£º


µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ     Get Users

ÕâÀïÎÒÃÇʹÓÃÁËÒ»¸ö±íµ¥À´·¢ËÍGETÇëÇó¡£±íµ¥µÄactionÊôÐÔָʾÁËÇëÇóµÄÄ¿µÄURL£¬¶ømethodÊôÐÔÔòÖ¸¶¨ÁËÇëÇó·½·¨ÎªGET¡£µ±Óû§µã»÷°´Å¥Ê±£¬ä¯ÀÀÆ÷»áÏòЧÀÍÆ÷·¢ËÍÒ»¸öGETÇëÇ󣬲¢ÔÚURLºó¼ÓÉÏÇëÇó²ÎÊý¡£ÀýÈ磬ÈôÊÇÎÒÃÇÔÚ±íµ¥ÖÐÊäÈëÁËÒ»¸öÃûΪ¡°John¡±µÄ²ÎÊý£¬ÄÇôÇëÇóµÄURL»áÄð³É/users?name=John¡£ÔÚЧÀÍÆ÷¶Ë£¬ÎÒÃÇ¿ÉÒÔʹÓÃ$request¹¤¾ßÀ´»ñÈ¡ÇëÇó²ÎÊý£º

Route::get('/users', function (Illuminate\Http\Request $request) {
   $name = $request->input('name');
   // ÅÌÎÊÊý¾Ý¿â£¬·µ»ØÇкÏÌõ¼þµÄÓû§Áбí
   $users = App\User::where('name', $name)->get();
   return view('users', ['users' => $users]);
});

µÇ¼ºó¸´ÖÆ

Õâ¶Î´úÂëչʾÁËÔõÑùʹÓÃ$request¹¤¾ßÀ´»ñÈ¡ÇëÇó²ÎÊý¡£ÎÒÃÇÊ×ÏÈŲÓÃÁËinput()ÒªÁìÀ´»ñÈ¡ÃûΪ¡°name¡±µÄ²ÎÊýÖµ£¬È»ºóʹÓÃËüÀ´ÅÌÎÊÊý¾Ý¿â£¬×îºó·µ»ØÇкÏÌõ¼þµÄÓû§Áбí¡£Õâ¸öÁÐ±í½«»á±»×ª´ïµ½ÏÈÇ°½ç˵µÄusersÊÓͼÖоÙÐÐչʾ¡£

POSTÇëÇó

POSTÇëÇóÊÇÓÃÓÚÏòЧÀÍÆ÷Ìá½»Êý¾ÝµÄÒ»ÖÖÇëÇó·½·¨¡£ËüµÄÇëÇó²ÎÊý»á¸½¼ÓÔÚÇëÇóÌåÖУ¬²¢ÒÔHTTP±¨ÎĵÄÐÎʽ·¢Ë͸øЧÀÍÆ÷¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute::post()ÒªÁìÀ´½ç˵һ¸öPOST·ÓÉ¡£ÀýÈ磺

Route::post('/users', function (Illuminate\Http\Request $request) {
   $name = $request->input('name');
   $email = $request->input('email');
   // ½«Óû§Êý¾ÝÉúÑĵ½Êý¾Ý¿â
   $user = new App\User;
   $user->name = $name;
   $user->email = $email;
   $user->save();
   return redirect('/users');
});

µÇ¼ºó¸´ÖÆ

Õâ¸ö·Óɽ«»áÆ¥Åä/users·¾¶£¬²¢½«ÎüÊÕµ½µÄPOSTÇëÇóÊý¾ÝÉúÑĵ½Êý¾Ý¿âÖС£ÔÚ±íµ¥Öз¢ËÍPOSTÇëÇóµÄ·½·¨Óë·¢ËÍGETÇëÇóÀàËÆ£¬Ö»Ð轫methodÊôÐÔµÄÖµ¸ÄΪ¡°post¡±¼´¿É£º


µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ    @csrf             Add User

ÕâÀïÎÒÃÇ»¹Ìí¼ÓÁËÒ»¸öÃûΪ¡°_token¡±µÄÒþ²Ø±íµ¥Óò(@csrf)¡£Õâ¸öÒþ²ØÓòÊÇLaravelµÄCSRF±£»¤¹¦Ð§Ëù±ØÐèµÄ£¬ËüÓÃÓÚ±ÜÃâ¿çÕ¾ÇëÇóαÔì¹¥»÷¡£ÔÚЧÀÍÆ÷¶Ë£¬ÎÒÃÇÐèÒªÔÚ·ÓÉÖÐʹÓÃIlluminate\Support\Facades\URL::csrfToken()ÒªÁìÌìÉúÒ»¸öCSRFÁîÅÆ£º

Route::post('/users', function () {
   return view('users');
})->middleware('web');

µÇ¼ºó¸´ÖÆ

Õâ¸ömiddlewareָʾÁËÇëÇóÐèÒª¾­ÓÉwebÖÐÑë¼þµÄ´¦Àí£¬webÖÐÑë¼þ»áΪÿ¸öÇëÇó×Ô¶¯Ìí¼ÓCSRFÁîÅÆ¡£

PUTºÍDELETEÇëÇó

PUTºÍDELETEÇëÇóÓÃÓÚ¸üкÍɾ³ýЧÀÍÆ÷¶ËµÄ×ÊÔ´£¬ËüÃÇÓëGETºÍPOSTÇëÇóÓÐ×ÅÀàËƵÄʹÓúʹ¦Àí·½·¨¡£ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃRoute::put()ºÍRoute::delete()ÒªÁìÀ´½ç˵PUTºÍDELETE·ÓÉ¡£ÀýÈ磺

Route::put('/users/{id}', function (Illuminate\Http\Request $request, $id) {
   $user = App\User::findOrFail($id);
   $user->name = $request->input('name');
   $user->email = $request->input('email');
   $user->save();
   return redirect('/users');
});

Route::delete('/users/{id}', function ($id) {
   $user = App\User::findOrFail($id);
   $user->delete();
   return redirect('/users');
});

µÇ¼ºó¸´ÖÆ

ÕâÀïÎÒÃǽç˵ÁËÒ»¸öPUT·ÓɺÍÒ»¸öDELETE·ÓÉ£¬ÓÃÓÚ¸üкÍɾ³ýÓû§ÐÅÏ¢¡£ÔÚ¿Í»§¶ËÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃJavaScript´úÂëÀ´·¢ËÍPUTºÍDELETEÇëÇó£º

// ·¢ËÍPUTÇëÇó
fetch('/users/1', {
   method: 'PUT',
   headers: {
      'Content-Type': 'application/json'
   },
   body: JSON.stringify({
      name: 'John Smith',
      email: 'john@example.com'
   })
}).then(response => {
   if (response.ok) {
      // Àֳɴ¦ÀíÏìÓ¦
   } else {
      // ´¦ÀíÏìÓ¦¹ýʧ
   }
}).catch(error => {
   // ´¦ÀíÍøÂçÇëÇó¹ýʧ
});

// ·¢ËÍDELETEÇëÇó
fetch('/users/1', {
   method: 'DELETE'
}).then(response => {
   if (response.ok) {
      // Àֳɴ¦ÀíÏìÓ¦
   } else {
      // ´¦ÀíÏìÓ¦¹ýʧ
   }
}).catch(error => {
   // ´¦ÀíÍøÂçÇëÇó¹ýʧ
});

µÇ¼ºó¸´ÖÆ

Õâ¸ö´úÂëչʾÁËÔõÑùʹÓÃfetch()º¯ÊýÀ´·¢ËÍPUTºÍDELETEÇëÇó¡£ÔÚ·¢ËÍPUTÇëÇóʱ£¬ÎÒÃǽ«ÇëÇóÌåÖеÄÊý¾Ýת»»ÎªJSONÃûÌ㬲¢ÔÚÇëÇóÍ·ÖÐÖ¸¶¨Content-TypeΪapplication/json¡£ÔÚЧÀÍÆ÷¶ËÖУ¬ÎÒÃÇͨ¹ýʹÓÃfindOrFail()ÒªÁìÀ´»ñÈ¡Óû§ÐÅÏ¢£¬²¢¸üлòɾ³ýÊý¾Ý¿âÖеļͼ¡£

×ܽá

LaravelÌṩÁ˶àÖÖ²î±ðµÄHTTPÇëÇó·½·¨£¬¿ÉÒÔÈÃÎÒÃÇÔ½·¢Àû±ãµØ´¦ÀíЧÀÍÆ÷¶ËµÄ×ÊÔ´¡£ÔÚ¿ª·¢WebÓ¦ÓóÌÐòʱ£¬ÎÒÃÇͨ³£»áʹÓöàÖÖÇëÇó·½·¨À´Íê³É²î±ðµÄʹÃü£¬ÀýÈçͨ¹ýGETÇëÇó»ñÈ¡Êý¾Ý¡¢Í¨¹ýPOSTÇëÇóÌá½»±íµ¥Êý¾ÝºÍͨ¹ýPUTÇëÇóºÍDELETEÇëÇó¸üкÍɾ³ý×ÊÔ´µÈ¡£Ê¹ÓÃLaravelµÄ·ÓÉϵͳ£¬ÎÒÃÇ¿ÉÒÔÇáËɵØΪ²î±ðµÄÇëÇó·½·¨½ç˵¶ÔÓ¦µÄ·ÓÉ£¬²¢ÔÚЧÀÍÆ÷¶Ë´¦ÀíÇëÇóÊý¾ÝºÍÏìӦЧ¹û¡£

ÒÔÉϾÍÊÇ̽ÌÖLaravelÖÐÖÖÖÖÇëÇó·½·¨µÄʹÓúʹ¦Àí·½·¨µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

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

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

QR code
ÍøÕ¾µØͼ