laravel ÖÐÐļþ²»ÉúЧÔõô°ì
ÔÚʹÓà laravel ¿ò¼Ü¿ª·¢Ó¦ÓÃʱ£¬ÈôÊÇÐèÒª¶ÔÇëÇó¾ÙÐÐһЩУÑé»ò´¦Àí£¬¿ÉÒÔʹÓÃÖÐÐļþ¡£ÖÐÐļþÊÇ laravel ÖÐÒ»¸öºÜÊÇÇ¿Ê¢µÄ¹¦Ð§£¬Ëü¿ÉÒÔ¶ÔÓ¦ÓõÄÇëÇó¾ÙÐÐÔ¤´¦Àí£¬ÒÔµÖ´ïһЩÌض¨µÄÄ¿µÄ£¬ºÃ±ÈÉí·ÝÑéÖ¤¡¢ÈÕÖ¾¼Í¼¡¢¿çÕ¾ÇëÇóαÔ죨csrf£©·ÀÓùµÈ¡£
È»¶ø£¬ÔÚʹÓà Laravel ÖÐÐļþʱ£¬ÓÐʱ¼äÎÒÃÇ»áÓöµ½Ò»Ð©ÎÊÌ⣬ÆäÖÐÖ®Ò»¼´ÊÇÖÐÐļþ²»ÉúЧµÄÇéÐΡ£ÖÐÐļþ²»ÉúЧµÄÔµ¹ÊÔÓÉ¿ÉÄÜÓÐÐí¶à£¬½ÓÏÂÀ´ÎÒÃÇǨ¾ÍһЩ³£¼ûµÄÇéÐÎÏ¢Õù¾öÒªÁì¾ÙÐÐÆÊÎöºÍ×ܽᡣ
ÖÐÐļþδע²á
ÔÚ Laravel ÖУ¬ÎÒÃÇÐèÒª½«×Ô½ç˵µÄÖÐÐļþ¾ÙÐÐ×¢²á²Å»ªÕý³£Ê¹Óá£ÈôÊÇÎÒÃÇÔÚ´úÂëÖÐûÓÐÍê³ÉÖÐÐļþµÄ×¢²á£¬ÄÇôÖÐÐļþÊDz»»áÉúЧµÄ¡£ÔÚ Laravel µÄ app/Http/Kernel.php ÎļþÖУ¬ÐèÒª½«×Ô½ç˵µÄÖÐÐļþ×¢²áµ½¶ÔÓ¦µÄ middleware Êý×éÖС£
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʾÀý´úÂ룺
namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middleware = [ // ... \App\Http\Middleware\MyMiddleware::class, ]; // ... }
µÇ¼ºó¸´ÖÆ
ÖÐÐļþ±»È«¾ÖÖÐÐļþ²ã´¦Àí
ÔÚ Laravel Ó¦ÓÃÖУ¬HTTP ÇëÇó»á¾ÓÉһϵÁеÄÖÐÐļþ´¦Àí£¬ÆäÖаüÀ¨È«¾ÖÖÐÐļþ²ã¡¢Â·ÓÉÖÐÐļþ²ãºÍ¿ØÖÆÆ÷ÖÐÐļþ²ã¡£ÈôÊÇÎÒÃǽç˵µÄÖÐÐļþ±»È«¾ÖÖÐÐļþ²ã×èµ²´¦ÀíÁË£¬ÄÇôËü¾ÍÎÞ·¨±»Â·ÓÉÖÐÐļþºÍ¿ØÖÆÆ÷ÖÐÐļþËù»á¼ûµ½£¬´Ó¶øµ¼ÖÂÖÐÐļþ²»ÉúЧ¡£
½â¾öÕâ¸öÎÊÌâÐèÒªÃ÷È·È«¾ÖÖÐÐļþµÄ×÷ÓùæÄ££¬ÒÔ¼°×Ô½ç˵ÖÐÐļþÐèÒªÔÚºÎʱºÎµØ¾ÙÐÐŲÓ᣹ØÓÚһЩÌØÊâµÄÖÐÐļþ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý¶ÔÆäÕë¶ÔÐԵķÓÉ»ò¿ØÖÆÆ÷¾ÙÐÐŲÓã¬×èֹȫ¾ÖÖÐÐļþµÄ×ÌÈÅ¡£
ÖÐÐļþ˳Ðò²»×¼È·
ÔÚ Laravel µÄÇëÇó´¦Àí¹ÜµÀ£¨Pipeline£©ÖУ¬ÖÐÐļþµÄ˳Ðò¿ÉÒÔ¾öÒéÖÐÐļþµÄ´¦Àí˳Ðò¡£ÈôÊÇÎÒÃǽ«ÖÐÐļþµÄ˳ÐòÉèÖùýʧ£¬ÄÇôҲ¿ÉÄܵ¼ÖÂÖÐÐļþ²»ÉúЧ¡£
ÔÚ Laravel Ó¦ÓÃÖУ¬ÖÐÐļþ»áƾ֤ÔÚ app/Http/Kernel.php ÖÐ×¢²áµÄ˳Ðò¾ÙÐÐŲÓá£ÈôÊÇijÆäÖÐÐļþÐèÒªÔÚÆäËûÖÐÐļþÖ´ÐÐ֮ǰÏÈÖ´ÐУ¬ÄÇô¾ÍÐèÒª½«Æä·ÅÔÚÊý×éµÄ¿ªÍ·¡£
ÏÂÃæÊÇÒ»¸öʾÀý´úÂ룺
namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // ... ]; // ... }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀý´úÂëÖУ¬StartSession ÖÐÐļþÐèÒªÔÚÆäËûµÄÖÐÐļþ֮ǰִÐУ¬Òò´Ë½«Ëü·ÅÔÚÁËÊý×éµÄĩβ¡£ÈôÊÇÎÒÃǽ« StartSession ÖÐÐļþ·ÅÔÚÆäËûÖÐÐļþÖ®ºó£¬ÄÇôËü¾Í»áÖ´Ðв»µ½£¬´Ó¶øµ¼ÖÂÖÐÐļþ²»ÉúЧ¡£
ÖÐÐļþδ¾ÓÉ·ÓÉȨÏÞ¿ØÖÆ
ÖÐÐļþÊÇ Laravel Ó¦ÓÃÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄÇå¾²¹¤¾ß£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃÇÑéÖ¤Óû§µÄÉí·Ý¡¢È¨ÏÞµÈÐÅÏ¢¡£È»¶ø£¬ÔÚijЩÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄܻᷢÃ÷ijЩÖÐÐļþ±»ÈƹýÁË·ÓÉȨÏÞ¿ØÖÆ£¬´Ó¶øµ¼ÖÂÇå¾²Òþ»¼¡£
½â¾öÕâ¸öÎÊÌâÐèÒª¶Ô Laravel ÖÐÐļþ¾ÙÐÐÉîÈëÏàʶ²¢ºÏÀíʹÓã¬È·±£ËüÃǵÄÏÖʵЧ¹ûÇкÏÎÒÃǵÄÔ¤ÆÚ¡£
×ܽá
ÖÐÐļþÊÇ Laravel Ó¦ÓÃÖеÄÒ»¸öºÜÊÇÖ÷ÒªµÄ×é¼þ£¬Ëü¿ÉÒÔ¶ÔÇëÇó¾ÙÐÐÔ¤´¦Àí£¬ÒÔµÖ´ïһЩÌض¨µÄÄ¿µÄ¡£µ«¹ØÓÚһЩÐÂÊÖ£¬ÓÐʱ¼ä¿ÉÄÜ»áÓöµ½ÖÐÐļþ²»ÉúЧµÄÇéÐΡ£ÔÚʹÓÃÖÐÐļþʱ£¬ÎÒÃÇÐèҪעÖØÖÐÐļþµÄ×¢²á¡¢È«¾ÖÖÐÐļþ²ãµÄ´¦Àí¡¢ÖÐÐļþµÄ˳Ðò¡¢ÒÔ¼°Â·ÓÉȨÏÞ¿ØÖƵÈÎÊÌ⣬´Ó¶ø×èֹһЩÄÑÒÔÔ¤ÁϵÄÇå¾²ÎÊÌâ¡£
Laravel ÖÐÐļþ²»ÉúЧ¿ÉÄܻᱬ·¢µÄÔµ¹ÊÔÓɲ¢²»Ö¹ÕâЩ£¬¸ü¶àϸ½ÚºÍÉîÈëÏàʶ»¹ÐèÒª¿ª·¢ÕßÃÇÔÚÏÖʵ¿ª·¢ÖоÙÐÐ̽Ë÷ºÍÑо¿¡£
ÒÔÉϾÍÊÇlaravel ÖÐÐļþ²»ÉúЧÔõô°ìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡