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

laravelÖÐÑë¼þµÄʵÏÖÀú³Ì

Ëæ×Å»¥ÁªÍøºÍwebÓ¦ÓõÄÒ»Ö±Éú³¤ £¬web¿ª·¢¿ò¼ÜÒ²Ò»Ö±Ó¿ÏÖ £¬ÆäÖÐ £¬laravel¿ò¼Ü»ñµÃÁËÆÕ±éµÄÈϿɺÍʹÓá£laravel¿ò¼ÜΪ¿ª·¢ÕßÌṩÁËÖÐÑë¼þÕâÒ»Ö÷ÒªµÄ¹¦Ð§ £¬ÔÚÓ¦ÓÃÖÐͨ¹ýÖÐÑë¼þ¶ÔÇëÇó¾ÙÐйýÂ˺ʹ¦Àí £¬ÓÐÓÃÌá¸ßÁË¿ª·¢Ð§ÂʺÍÇå¾²ÐÔ¡£±¾ÎĽ«ÏêϸÏÈÈÝlaravelÖÐÑë¼þµÄʵÏÖÀú³Ì £¬ÒÔ×ÊÖú¿ª·¢Õ߸üºÃµØÃ÷È·ºÍÓ¦ÓÃÕâÒ»¹¦Ð§¡£

Ò»¡¢Ê²Ã´ÊÇÖÐÑë¼þ

ÔÚLaravel¿ò¼ÜÖÐ £¬ÖÐÑë¼þ£¨Middleware£©Ö¸µÄÊǶԽøÈëÓ¦ÓõÄHTTPÇëÇó¾ÙÐйýÂ˺ʹ¦ÀíµÄÒ»ÖÖ»úÖÆ £¬ÀàËÆÓÚ´úÂëÖг£¼ûµÄ×èµ²Æ÷ºÍ¹ýÂËÆ÷µÄ¿´·¨¡£Ò»Ñùƽ³£ÇéÐÎÏ £¬ÖÐÑë¼þ»á×öһЩͨÓõĴ¦ÀíÊÂÇé £¬ºÃ±È£º

ÈÏÕæ¶ÔÇëÇó¾ÙÐÐÉí·ÝÑéÖ¤£»

¼ì²é²¢ÏÞÖÆÇëÇóµÄ»á¼ûƵÂÊ£»

ά»¤»á»°×´Ì¬£»

ƾ֤ÇëÇóÖеÄHeaderÐÅÏ¢ £¬ÉèÖöÔÓ¦µÄÏìÓ¦HeaderµÈ¡£

Laravel¿ò¼ÜÌṩÁËһЩÄÚÖõÄÖÐÑë¼þ £¬ºÃ±ÈÉí·ÝÑéÖ¤ÖÐÑë¼þ¡¢CSRFÖÐÑë¼þ¡¢ÏÞÁ÷ÖÐÑë¼þµÈ £¬¿ª·¢ÕßÒ²¿ÉÒÔ×ÔÐбàд×Ô½ç˵ÖÐÑë¼þÀ´Öª×ãÓ¦ÓõÄÌض¨ÐèÇó¡£

¶þ¡¢ÖÐÑë¼þµÄʵÏÖÔ­Àí

Laravel¿ò¼ÜµÄÖÐÑë¼þ»úÖÆÊÇ»ùÓÚPSR-15±ê׼ʵÏֵġ£ÔÚLaravelÖÐ £¬ÖÐÑë¼þÒ»Ñùƽ³£°üÀ¨Á½¸ö²¿·Ö£ºÖÐÑë¼þÀàºÍÖÐÑë¼þŲÓ᣽ÓÏÂÀ´ £¬ÎÒÃÇ»®·Ö¶ÔÕâÁ½²¿·Ö¾ÙÐÐÏêϸÏÈÈÝ¡£

ÖÐÑë¼þÀà

ÖÐÑë¼þÀàÊÇʵÏÖÖÐÑë¼þÂß¼­µÄ½¹µã²¿·Ö £¬Ò»Ñùƽ³£ÐèҪʵÏÖPSR-15Öнç˵µÄMiddlewareInterface½Ó¿ÚÖеÄprocessÒªÁì¡£¸ÃÒªÁì½ÓÊÜÁ½¸ö²ÎÊý £¬¼´Ä¿½ñÇëÇ󹤾ߺÍÒ»¸ö»Øµ÷º¯Êý £¬»Øµ÷º¯ÊýÈÏÕ潫ÇëÇóת´ï¸øÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò¡£

ÖÐÑë¼þµÄ´¦ÀíÂß¼­Ò»Ñùƽ³£ÔÚprocessÒªÁìÖÐʵÏÖ £¬¿ÉÒÔ¶ÁÈ¡ÇëÇóÊý¾Ý £¬ÐÞ¸ÄÏìÓ¦Êý¾Ý £¬ÒÔ¼°Ö´ÐÐÆäËûÌض¨µÄ²Ù×÷¡£Ê¾Àý´úÂëÈçÏ£º

<?php

namespace AppHttpMiddleware;

use Closure;
use IlluminateHttpRequest;

class MyMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        // ÅжÏÄ¿½ñÇëÇóÊÇ·ñЯ´øtoken
        if (!$request->header('Authorization')) {
            return response()->json(['message' => 'Unauthorized'], 401);
        }

        // Ö´ÐÐÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò
        return $next($request);
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖÐ £¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪMyMiddlewareµÄÖÐÑë¼þÀà £¬ËüÅжÏÇëÇóÊÇ·ñЯ´øAuthorizationÍ·²¿ÐÅÏ¢ £¬ÈôÊÇδЯ´ø £¬Ôò·µ»ØδÊÚȨµÄÏìÓ¦£»ÈôÊÇÒÑЯ´ø £¬ÔòÖ´ÐÐÏÂÒ»ÆäÖÐÑë¼þ»òÓ¦ÓóÌÐò¡£

ÖÐÑë¼þŲÓÃ

ÔÚLaravel¿ò¼ÜÖÐÒýÈëÖÐÑë¼þµÄ·½·¨ºÜÊÇÎÞа £¬¿ª·¢Õß¿ÉÒÔÕë¶Ô²î±ðµÄÇëÇ󷾶¡¢ÇëÇóÒªÁì¡¢ÇëÇóÀàÐ͵ÈÌõ¼þÒýÈë²î±ðµÄÖÐÑë¼þ £¬Ò²¿ÉÒÔͨ¹ýÖÐÑë¼þ×éµÄ·½·¨ÅúÁ¿ÒýÈë¡£ÏÂÃæÊǼ¸ÖÖ³£¼ûµÄÒýÈë·½·¨£º

ÒýÈëÈ«¾ÖÖÐÑë¼þ£º½«ÖÐÑë¼þÌí¼Óµ½app/Http/Kernel.phpÎļþµÄ$middlewareÊôÐÔÖÐ £¬Ëü½«Ó¦ÓÃÓÚËùÓеÄÇëÇó¡£

// app/Http/Kernel.php

protected $middleware = [
    AppHttpMiddlewareMyMiddleware::class,
];

µÇ¼ºó¸´ÖÆ

ÒýÈë·ÓÉÖÐÑë¼þ£ºÔÚroutes/web.phpÎļþÖнç˵·ÓÉʱ £¬¿ÉÒÔͨ¹ýmiddlewareÒªÁìÀ´ÒýÈëÖ¸¶¨µÄÖÐÑë¼þ¡£

// routes/web.php

use AppHttpMiddlewareMyMiddleware;

Route::get('/user', function () {
    //
})->middleware(MyMiddleware::class);

µÇ¼ºó¸´ÖÆ

³ý´ËÖ®Íâ £¬»¹¿ÉÒÔͨ¹ýÖÐÑë¼þ×飨Middleware Groups£©µÄ·½·¨ÅúÁ¿ÒýÈë¶àÆäÖÐÑë¼þ £¬ÒÔ´ËΪ»ù´¡ £¬ÎÞаӦÓÃÖÐÑë¼þ £¬ÔÚ²î±ðµÄ³¡¾°ÏÂʵÏÖ²î±ðµÄ¹¦Ð§¡£

Èý¡¢ÖÐÑë¼þÖ´ÐÐÁ÷³Ì

ÔÚÇëÇó½øÈëLaravelÓ¦ÓóÌÐòʱ £¬Laravel¿ò¼Üƾ֤ÖÐÑë¼þµÄÒýÈë˳ÐòŲÓÃÕâЩÖÐÑë¼þÀà £¬È»ºóÖ´ÐÐËüÃǵÄprocessÒªÁì¡£ÓÉÓÚÖÐÑë¼þÁ´µÄ×é³É £¬¿ÉÒÔÇáËɵØÔÚÇëÇó±»´¦Àí֮ǰ»òÖ®ºó¾ÙÐвÙ×÷¡£

Laravel¿ò¼ÜµÄÖÐÑë¼þµ÷ÀíÁ÷³ÌÈçÏ£º

ÇëÇó½øÈëÓ¦Óà £¬¾­ÓÉHttpKernelÖÐÑë¼þ´¦Àí £¬½¨ÉèÇëÇ󹤾ߣ¨IlluminateHttpRequestʵÀý£©¡£

ŲÓÃÈ«¾ÖÖÐÑë¼þ £¬ÈôÊÇÓÐ×îÏȵÄÖÐÑë¼þÐèÒªÖ´ÐÐ £¬ÔòÖ´ÐиÃÖÐÑë¼þµÄprocessÒªÁì¡£

½Ó×ÅŲÓ÷ÓÉÖÐÑë¼þ £¬ÈôÊÇÓÐ×îÏȵÄÖÐÑë¼þÐèÒªÖ´ÐÐ £¬ÔòÖ´ÐиÃÖÐÑë¼þµÄprocessÒªÁì¡£

ÇëÇó×îÖÕµÖ´ï·Óɱհüº¯Êý»ò¿ØÖÆÆ÷ÒªÁì £¬Íê³ÉÏêϸµÄÓªÒµÂß¼­´¦Àí¡£

¾­ÓÉ·ÓÉÖÐÑë¼þµÄprocessÒªÁì·µ»Øµ½HttpKernelÖÐÑë¼þ £¬°´Ïà·´µÄ˳ÐòŲÓ÷ÓÉÖÐÑë¼þºÍÈ«¾ÖÖÐÑë¼þµÄterminateÒªÁì¡£

×îÖÕÌìÉúHTTPÏìÓ¦ £¬·µ»Ø¸øä¯ÀÀÆ÷¡£

Laravel¿ò¼ÜµÄÖÐÑë¼þÊÇÒ»¸öºÜÊÇÓÐÓõĹ¦Ð§ £¬¿ÉÒÔÇáËɵØÔÚÇëÇó´¦ÀíÀú³ÌÖÐÖ´ÐÐijЩ´úÂë»ò¼ì²âÇëÇó £¬´Ó¶øΪӦÓóÌÐòÔöÌíÁËÎÞаÐÔºÍÇå¾²ÐÔ¡£Í¨¹ý¶ÔLaravelÖÐÑë¼þµÄÏÈÈݺÍʵÏÖÔ­ÀíµÄÏàʶ £¬¿ª·¢Õß¿ÉÒÔ¸üÉîÈëµØÃ÷È·ºÍÓ¦ÓÃÖÐÑë¼þµÄ×÷Óá£

ÒÔÉϾÍÊÇlaravelÖÐÑë¼þµÄʵÏÖÀú³ÌµÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ