̽ÌÖLaravelÖÐ±íµ¥Ìá½»µÄÏà¹Ø֪ʶ
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ÌṩÁËÐí¶àÀû±ãµÄ¹¦Ð§À´¿ìËÙ¹¹½¨webÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸ö³£¼ûµÄ¹¦Ð§¾ÍÊÇ´¦Àí±íµ¥Ìá½»¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÉîÈë̽ÌÖlaravelÖÐ±íµ¥Ìá½»µÄÏà¹Ø֪ʶ¡£
½¨Éè±íµ¥
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÍøÒ³Öн¨ÉèÒ»¸ö±íµ¥¡£LaravelÌṩÁËÐí¶à±íµ¥¹¹½¨ÒªÁ죬×î³£ÓõÄÊÇʹÓÃBladeÊÓͼÀ´ÌìÉú±íµ¥¡£ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÀý×Ó£º
µÇ¼ºó¸´ÖÆ @csrf ÐÕÃû£º Ìá½»
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇʹÓÃ
±êÇ©À´½¨ÉèÒ»¸ö±íµ¥£¬²¢ÇÒÉèÖÃÁË±íµ¥µÄmethodºÍactionÊôÐÔ¡£methodÖ¸¶¨±íµ¥Ìá½»·½·¨£¬³£ÓõÄÓÐGETºÍPOSTÁ½ÖÖ¡£actionÖ¸¶¨±íµ¥Ìá½»µÄÄ¿µÄµØµã£¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÌṩµÄrouteº¯ÊýÀ´ÌìÉú·Óɵص㡣
ͬʱ£¬ÎÒÃÇÒ²ÐèÒªÌí¼ÓÒ»¸öÒþ²Ø×ֶΣ¬ÒÔ±ãÑéÖ¤±íµ¥Ìá½»ÇëÇóµÄÇå¾²ÐÔ¡£LaravelÌṩÁËÒ»¸ö@csrfÖ¸ÁîÀ´ÌìÉúÕâ¸öÒþ²Ø×ֶΡ£
ÔÚ±íµ¥ÖУ¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÖÖÖÖÀàÐÍµÄ±íµ¥Î»ËØ£¬ÈçÎı¾¿ò¡¢ÏÂÀ¿ò¡¢µ¥Ñ¡¿ò¡¢¸´Ñ¡¿òµÈ¡£ÕâЩԪËض¼¿ÉÒÔͨ¹ý¡¢ºÍ<select>µÈ±êÇ©À´½¨Éè¡£</select>
´¦Àí±íµ¥Ìá½»ÇëÇó
µ±Óû§Ìá½»±íµ¥ºó£¬ÎÒÃÇÐèÒªÔÚºó¶Ë¾ÙÐд¦Àí¡£LaravelÌṩÁ˺ÜÀû±ãµÄ·½·¨À´´¦Àí±íµ¥Ìá½»ÇëÇó¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ·Óɽç˵ÖÐÌí¼ÓÒ»¸ö´¦ÀíÇëÇóµÄÒªÁ죬ÀýÈ磺
Route::post('/submit', 'FormController@submit')->name('submit');
µÇ¼ºó¸´ÖÆ
Õâ¸ö·Óɽç˵ÌåÏÖµ±Óû§ÒÔPOST·½·¨»á¼û/submitµØµãʱ£¬»áŲÓÃFormControllerÖеÄsubmitÒªÁì¾ÙÐд¦Àí¡£ÎÒÃÇ»¹¸øÕâ¸ö·ÓÉÆðÁËÒ»¸öÃû×Ö£¬Àû±ãÔÚÊÓͼÖÐÌìÉú±íµ¥µÄactionÊôÐÔ¡£
½Ó×Å£¬ÎÒÃÇÐèÒªÔÚFormControllerÖнç˵submitÒªÁìÀ´´¦Àí±íµ¥Ìá½»ÇëÇó¡£ÀýÈ磺
use Illuminate\Http\Request; class FormController extends Controller { public function submit(Request $request) { $name = $request->input('name'); // ´¦Àí±íµ¥Êý¾Ý return view('submit-success'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇÊ×ÏÈͨ¹ýÒÀÀµ×¢ÈëµÄ·½·¨»ñÈ¡ÁËRequest¹¤¾ß¡£Õâ¸ö¹¤¾ß¿ÉÒÔÓÃÀ´»ñÈ¡±íµ¥Ìá½»µÄÊý¾Ý¡£
ÀýÈ磬ÎÒÃÇʹÓÃ$request->input(‘name’)À´»ñÈ¡ÃûΪnameµÄ±íµ¥Î»ËصÄÖµ¡£
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ¶Ô±íµ¥Êý¾Ý¾ÙÐд¦Àí£¬ÀýÈç´æ´¢µ½Êý¾Ý¿âÖС£×îºó£¬ÎÒÃÇ·µ»ØÒ»¸öÊÓͼÀ´Õ¹Ê¾±íµ¥Ìá½»ÀֳɵÄÒ³Ãæ¡£
±íµ¥ÑéÖ¤
ÔÚ´¦Àí±íµ¥Ìá½»ÇëÇóʱ£¬ÎÒÃÇͨ³£»¹ÐèÒª¶ÔÓû§ÊäÈëÊý¾Ý¾ÙÐÐÑéÖ¤£¬ÒÔÈ·±£Êý¾ÝµÄÕýµ±ÐÔ¡£LaravelÌṩÁ˺ÜÀû±ãµÄ·½·¨À´¾ÙÐÐ±íµ¥ÑéÖ¤¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚFormControllerÖнç˵һ¸örulesÒªÁ죬ÓÃÀ´½ç˵ÑéÖ¤¹æÔò¡£ÀýÈ磺
class FormController extends Controller { public function rules() { return [ 'name' => 'required|max:255', 'email' => 'required|email|unique:users', 'password' => 'required|confirmed|min:6', ]; } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇ·µ»ØÁËÒ»¸öÊý×飬ÆäÖÐÿ¸öÔªËØÌåÏÖÒ»¸ö±íµ¥Î»ËصÄÑéÖ¤¹æÔò¡£ÀýÈ磬nameÔªËصÄÑéÖ¤¹æÔòÌåÏÖËüÊDZØÌîµÄ£¬²¢ÇÒ³¤¶È²»Áè¼Ý255¸ö×Ö·û¡£
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÐÞ¸ÄsubmitÒªÁ죬ʹÓÃvalidateÒªÁìÀ´¾ÙÐÐ±íµ¥ÑéÖ¤¡£ÀýÈ磺
public function submit(Request $request) { $validatedData = $request->validate($this->rules()); // ´¦ÀíÑé֤ͨ¹ýµÄ±íµ¥Êý¾Ý return view('submit-success'); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÒªÁìÖУ¬ÎÒÃÇÊ×ÏÈŲÓÃÁËrulesÒªÁìÀ´»ñÈ¡ÑéÖ¤¹æÔò¡£È»ºó£¬ÎÒÃÇŲÓÃÁË$request->validateÒªÁìÀ´¾ÙÐÐ±íµ¥ÑéÖ¤¡£ÈôÊÇÑé֤ʧ°Ü£¬Laravel»á×Ô¶¯·µ»ØÒ»¸ö°üÀ¨¹ýʧÐÅÏ¢µÄÏìÓ¦¡£ÈôÊÇÑé֤ͨ¹ý£¬Laravel»á·µ»ØÑé֤ͨ¹ýµÄ±íµ¥Êý¾Ý£¬ÎÒÃÇ¿ÉÒÔÔÚÖ®ºóµÄ´¦ÀíÖÐʹÓá£
ÐèҪעÖصÄÊÇ£¬validateÒªÌå»á×Ô¶¯Ê¹ÓÃrulesÒªÁìÖнç˵µÄÑéÖ¤¹æÔò¾ÙÐÐÑéÖ¤£¬ÈôÊÇÓйýʧÐÅÏ¢£¬Laravel»á×Ô¶¯½«ÕâЩ¹ýʧÐÅÏ¢Ìí¼Óµ½$errors±äÁ¿ÖУ¬ÎÒÃÇ¿ÉÒÔÔÚÊÓͼÖÐʹÓÃ$errors->first(‘name’)À´»ñÈ¡ÃûΪnameµÄ±íµ¥Î»ËصĵÚÒ»¸ö¹ýʧÐÅÏ¢¡£
×ܽá
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÔÚLaravelÖÐÀû±ãµØ´¦Àí±íµ¥Ìá½»ÇëÇó¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÊÓͼÖн¨Éè±íµ¥£¬²¢Ê¹ÓÃBladeÖ¸ÁîÀ´ÉèÖÃ±íµ¥Î»ËØ¡£È»ºó£¬ÎÒÃÇÔÚ·Óɽç˵ÖÐÌí¼Ó´¦ÀíÇëÇóµÄÒªÁ죬ʹÓÃRequest¹¤¾ßÀ´»ñÈ¡±íµ¥Êý¾Ý£¬²¢¶ÔÊý¾Ý¾ÙÐд¦Àí¡£×îºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃrulesÒªÁìÀ´½ç˵±íµ¥ÑéÖ¤¹æÔò£¬²¢Ê¹ÓÃvalidateÒªÁìÀ´¾ÙÐÐÑéÖ¤¡£
ÐèҪעÖصÄÊÇ£¬LaravelÌṩÁËÐí¶àÆäËûµÄ±íµ¥´¦Àí·½·¨£¬ÀýÈçʹÓÃFormÀàºÍRequest¹¤¾ßÀ´½¨Éè±íµ¥¡¢Ê¹ÓÃRequest¹¤¾ß¾ÙÐÐ±íµ¥ÑéÖ¤µÈ¡£ÎÒÃÇ¿ÉÒÔƾ֤ÏêϸÐèÇóÑ¡Ôñ²î±ðµÄ·½·¨À´¾ÙÐÐ±íµ¥´¦Àí¡£
ÒÔÉϾÍÊÇ̽ÌÖLaravelÖÐ±íµ¥Ìá½»µÄÏà¹Ø֪ʶµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡