尊龙凯时人生就是博

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
sitemap¡¢ÍøÕ¾µØͼ