laravelÐÂÔö²»¿É±à¼Ôõô»ØÊÂ
×î½üÔÚʹÓà laravel ¿ò¼Ü¿ª·¢ÏîĿʱ£¬·¢Ã÷Ò»¸öÎÊÌ⣺ÐÂÔöµÄÊý¾ÝÎÞ·¨±à¼¡£ÔÚ½øÒ»²½ÅŲéºÍÆÊÎöÖ®ºó£¬ÎÒ·¢Ã÷ÁËÎÊÌâËùÔÚ²¢½â¾öÁËÕâ¸öÎÊÌâ¡£ÏÂÃæ¾Í·ÖÏíһϽâ¾ö˼Ð÷¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÃ÷È·Ò»¸ö¿´·¨¡ª¡ªHTTP ÇëÇóÒªÁì¡£HTTP ÇëÇóÒªÁìÊÇÖ¸¿Í»§¶ËÔÚÏòЧÀÍÆ÷ÇëÇó×ÊԴʱËùʹÓõÄÒªÁ죬°üÀ¨ GET¡¢POST¡¢PUT¡¢DELETE µÈ¡£ÆäÖУ¬GET ÒªÁìÓÃÓÚ»ñÈ¡×ÊÔ´£¬POST ÒªÁìÓÃÓÚ½¨Éè×ÊÔ´£¬PUT ÒªÁìÓÃÓÚ¸üÐÂ×ÊÔ´£¬DELETE ÒªÁìÓÃÓÚɾ³ý×ÊÔ´¡£
ÔÚ Laravel ÖУ¬Ê¹ÓÃ±íµ¥Ìύʱ£¬Ä¬ÈÏÇëÇóÒªÁìÊÇ POST£¬ÀýÈ磺
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ Ìá½»
È»ºó£¬ÎÒÃÇÔÚ¿ØÖÆÆ÷ÖÐʹÓà store ÒªÁìÀ´´¦ÀíÕâ¸öÇëÇó£º
public function store(Request $request) { $user = new User; $user->name = $request->input('name'); $user->save(); return redirect('/user'); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÐ嵀 User ¹¤¾ß£¬½«±íµ¥Ìá½»µÄ name ×ֶθ³Öµ¸ø User ¹¤¾ßµÄ name ÊôÐÔ£¬È»ºóÉúÑÄÊý¾Ý²¢Öض¨Ïòµ½ /user Ò³Ãæ¡£
¿ÉÊÇ£¬ÉÏÊö´úÂëÓÐʱ¼ä»á·ºÆðÒ»¸öÎÊÌ⣺ÐÂÔöµÄÊý¾ÝÎÞ·¨±à¼¡£ÎÒÃÇÔÚ»á¼û±à¼Ò³Ãæʱ£¬»á·¢Ã÷Ò³ÃæÖÐûÓÐÔÏȽ¨ÉèµÄÊý¾ÝÐÅÏ¢¡£ÕâÊÇΪʲôÄØ£¿ÎÒÃÇ¿ÉÒÔÏÈ¿´Ò»Ï±à¼Ò³ÃæµÄ´úÂ룺
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ id }}” method=”POST”> {{ method_field(‘PUT’) }} name }}”> Ìá½»
ÔÚÕâ¸ö´úÂëÖУ¬ÎÒÃÇʹÓÃÁË method_field ÒªÁìÀ´Ö¸¶¨ÇëÇóÒªÁìΪ PUT£¬ÓÉÓÚÔÚ HTTP ÐÒéÖУ¬¸üÐÂ×ÊÔ´ÐèҪʹÓà PUT ÒªÁì¡£¿ÉÊÇ£¬ÔÚ Laravel ¿ò¼ÜÖУ¬Ä¬È쵀 POST ÇëÇóÊÇÎÞ·¨Ê¶±ð PUT ÇëÇóµÄ£¬Òò´Ë£¬ÔÚ»á¼û±à¼Ò³Ãæʱ£¬Laravel »áƾ֤ĬÈ쵀 POST ÇëÇóÀ´´¦Àí£¬¶ø²»ÊÇƾ֤ÎÒÃÇÆÚÍûµÄ PUT ÇëÇóÀ´´¦Àí¡£
½â¾öÕâ¸öÎÊÌâµÄ˼Ð÷ÓÐÁ½¸ö£ºÒ»ÊÇʹÓà PUT ÇëÇóÒªÁìÀ´½¨ÉèÊý¾Ý£»¶þÊÇÔÚʹÓà POST ÒªÁ콨ÉèÊý¾Ýʱ£¬Ê¹Óà _method ²ÎÊýÀ´Ö¸¶¨ÇëÇóÒªÁì¡£
µÚÒ»ÖÖ½â¾ö˼Ð÷ÊǽÏÁ¿Ë¬ÐÔµÄÒªÁ죬¿ÉÊÇÐèÒª¸Ä¶¯½Ï¶àµÄ´úÂë¡£ÎÒÃÇÐèÒªÔÚ±íµ¥ÖÐʹÓà PUT ÇëÇóÒªÁ죺
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ id }}” method=”POST”> {{ method_field(‘PUT’) }} name }}”> Ìá½»
È»ºó£¬ÔÚ¿ØÖÆÆ÷Öд¦Àí PUT ÇëÇó£º
public function update(Request $request, $id) { $user = User::findOrFail($id); $user->name = $request->input('name'); $user->save(); return redirect('/user'); }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ʹÓÃÕâÖÖ·½·¨£¬ÎÒÃÇÐèÒªÔÚ±íµ¥ÖÐÖ¸¶¨½ÓÄÉ PUT ÒªÁ죬²¢ÇÒÐèҪת´ïÒ»¸ö _token ²ÎÊýÀ´±ÜÃâ CSRF ¹¥»÷£¬ÕâÑù»áÔöÌí²»ÉٵĴúÂëÊÂÇéÁ¿¡£
µÚ¶þÖÖ½â¾ö˼Ð÷ÔòÒª¼òÆӵöࡣÎÒÃÇÐèÒªÔÚ±íµ¥ÖÐʹÓà POST ÒªÁ죬¿ÉÊÇÔÚÌύʱ¼ÓÉÏ _method ²ÎÊýÖ¸¶¨ÇëÇóÒªÁìΪ PUT£º
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ id }}” method=”POST”> name }}”> Ìá½»
È»ºó£¬ÔÚ¿ØÖÆÆ÷Öд¦Àí POST ÇëÇóʱ£¬Ê¹Óà method ÒªÁìÀ´Ê¶±ð PUT ÇëÇó£º
public function update(Request $request, $id) { $user = User::findOrFail($id); $user->name = $request->input('name'); $user->save(); return redirect('/user'); }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ʹÓÃÕâÖÖ·½·¨£¬ÎÒÃÇÖ»ÐèÒªÔÚ±íµ¥ÖмÓÉÏÒ»¸ö _method ²ÎÊý¼´¿É£¬¿É¶ÁÐԽϸߣ¬´úÂëÊÂÇéÁ¿ÉÙ¡£
ÔÚ¿ª·¢ Laravel ÏîÄ¿µÄÀú³ÌÖУ¬Óöµ½ÎÊÌâÊÇÒ»¶¨µÄ£¬½â¾öÎÊÌâÐèÒªÎÒÃÇÄÍÐĺÍ×Ðϸ£¬ÒÔ¼°¶Ô¿ò¼ÜºÍÊÖÒÕµÄÉîÈëÃ÷È·ºÍÕÆÎÕ¡£Ï£Íû±¾ÎÄÄÜΪ¶ÁÕßÌṩһЩ²Î¿¼ºÍ×ÊÖú¡£
ÒÔÉϾÍÊÇlaravelÐÂÔö²»¿É±à¼Ôõô»ØʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡