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

̽ÌÖ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')-&gt;name('submit');

µÇ¼ºó¸´ÖÆ

Õâ¸ö·Óɽç˵ÌåÏÖµ±Óû§ÒÔPOST·½·¨»á¼û/submitµØµãʱ £¬»áŲÓÃFormControllerÖеÄsubmitÒªÁì¾ÙÐд¦Àí¡£ÎÒÃÇ»¹¸øÕâ¸ö·ÓÉÆðÁËÒ»¸öÃû×Ö £¬Àû±ãÔÚÊÓͼÖÐÌìÉú±íµ¥µÄactionÊôÐÔ¡£

½Ó×Å £¬ÎÒÃÇÐèÒªÔÚFormControllerÖнç˵submitÒªÁìÀ´´¦Àí±íµ¥Ìá½»ÇëÇó¡£ÀýÈ磺

use Illuminate\Http\Request;

class FormController extends Controller
{
    public function submit(Request $request)
    {
        $name = $request-&gt;input('name');

        // ´¦Àí±íµ¥Êý¾Ý

        return view('submit-success');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÒªÁìÖÐ £¬ÎÒÃÇÊ×ÏÈͨ¹ýÒÀÀµ×¢ÈëµÄ·½·¨»ñÈ¡ÁËRequest¹¤¾ß¡£Õâ¸ö¹¤¾ß¿ÉÒÔÓÃÀ´»ñÈ¡±íµ¥Ìá½»µÄÊý¾Ý¡£

ÀýÈç £¬ÎÒÃÇʹÓÃ$request->input(‘name’)À´»ñÈ¡ÃûΪnameµÄ±íµ¥Î»ËصÄÖµ¡£

½ÓÏÂÀ´ £¬ÎÒÃÇ¿ÉÒÔ¶Ô±íµ¥Êý¾Ý¾ÙÐд¦Àí £¬ÀýÈç´æ´¢µ½Êý¾Ý¿âÖС£×îºó £¬ÎÒÃÇ·µ»ØÒ»¸öÊÓͼÀ´Õ¹Ê¾±íµ¥Ìá½»ÀֳɵÄÒ³Ãæ¡£

±íµ¥ÑéÖ¤

ÔÚ´¦Àí±íµ¥Ìá½»ÇëÇóʱ £¬ÎÒÃÇͨ³£»¹ÐèÒª¶ÔÓû§ÊäÈëÊý¾Ý¾ÙÐÐÑéÖ¤ £¬ÒÔÈ·±£Êý¾ÝµÄÕýµ±ÐÔ¡£LaravelÌṩÁ˺ÜÀû±ãµÄ·½·¨À´¾ÙÐÐ±íµ¥ÑéÖ¤¡£

Ê×ÏÈ £¬ÎÒÃÇÐèÒªÔÚFormControllerÖнç˵һ¸örulesÒªÁì £¬ÓÃÀ´½ç˵ÑéÖ¤¹æÔò¡£ÀýÈ磺

class FormController extends Controller
{
    public function rules()
    {
        return [
            'name' =&gt; 'required|max:255',
            'email' =&gt; 'required|email|unique:users',
            'password' =&gt; 'required|confirmed|min:6',
        ];
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚÕâ¸öÒªÁìÖÐ £¬ÎÒÃÇ·µ»ØÁËÒ»¸öÊý×é £¬ÆäÖÐÿ¸öÔªËØÌåÏÖÒ»¸ö±íµ¥Î»ËصÄÑéÖ¤¹æÔò¡£ÀýÈç £¬nameÔªËصÄÑéÖ¤¹æÔòÌåÏÖËüÊDZØÌîµÄ £¬²¢ÇÒ³¤¶È²»Áè¼Ý255¸ö×Ö·û¡£

½ÓÏÂÀ´ £¬ÎÒÃÇÐèÒªÐÞ¸ÄsubmitÒªÁì £¬Ê¹ÓÃvalidateÒªÁìÀ´¾ÙÐÐ±íµ¥ÑéÖ¤¡£ÀýÈ磺

public function submit(Request $request)
{
    $validatedData = $request-&gt;validate($this-&gt;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ÖÐ±íµ¥Ìá½»µÄÏà¹Ø֪ʶµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ