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

ÔõÑùÔÚThinkPHP6ÖÐʹÓÃÖÐÑë¼þ¾ÙÐÐÇëÇó´¦Àí£¿

ÔÚthinkphp6ÖÐ £¬ÖÐÑë¼þÊÇÒ»ÖÖ³£ÓõÄÇëÇó´¦Àí¹¤¾ß ¡£Í¨¹ýÖÐÑë¼þ £¬ÎÒÃÇ¿ÉÒÔÀû±ãµØ¶ÔÇëÇó¾ÙÐд¦Àí £¬´Ó¶øʵÏÖȨÏÞ¿ØÖÆ¡¢ÈÕÖ¾¼Í¼¡¢ÇëÇóÑéÖ¤µÈ¹¦Ð§ £¬Ìá¸ßÁËÓ¦ÓóÌÐòµÄÇå¾²ÐԺͿÉά»¤ÐÔ ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6ÖÐʹÓÃÖÐÑë¼þ¾ÙÐÐÇëÇó´¦Àí ¡£

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

ÖÐÑë¼þÊÇÒ»ÖÖ×ÔÁ¦µÄÇëÇó´¦Àí³ÌÐò £¬Ëü¿ÉÒÔ×èµ²¿Í»§¶Ë·¢³öµÄÇëÇó²¢¾ÙÐд¦Àí ¡£ÔÚThinkPHP6ÖÐ £¬ÖÐÑë¼þÊÇ»ùÓÚPSR-15¹æ·¶Éè¼ÆµÄ £¬Í¨¹ýÖÐÑë¼þ £¬ÎÒÃÇ¿ÉÒÔ¶ÔÇëÇó¾ÙÐÐÔ¤´¦Àí»òÕߺó´¦Àí £¬´Ó¶øʵÏÖһЩ³£ÓõĹ¦Ð§ £¬ÈçµÇ¼ÑéÖ¤¡¢È¨ÏÞ¿ØÖÆ¡¢ÇëÇó¹ýÂË¡¢ÏìÓ¦´¦ÀíµÈ ¡£

¶þ¡¢ThinkPHP6ÖÐÑë¼þµÄʹÓÃ

½¨ÉèÖÐÑë¼þ

ÔÚThinkPHP6ÖÐ £¬¿ÉÒÔͨ¹ýÏÂÁîÐй¤¾ß¿ìËÙ½¨ÉèÖÐÑë¼þ ¡£Ê¹ÓÃÒÔÏÂÏÂÁî¿ÉÒÔÔÚapp/middlewareĿ¼Ï½¨ÉèÒ»¸öÃûΪCheckAuthµÄÖÐÑë¼þ ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

php think make:middleware CheckAuth

µÇ¼ºó¸´ÖÆ

½¨ÉèºÃµÄCheckAuthÖÐÑë¼þÀàÈçÏ£º

<?php

namespace appmiddleware;

class CheckAuth
{
    public function handle($request, Closure $next)
    {
        // Ö´ÐÐÖÐÑë¼þ²Ù×÷

        return $next($request);
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬ÖÐÑë¼þÀà±ØÐè°üÀ¨handleÒªÁì ¡£handleÒªÁì½ÓÊÜÁ½¸ö²ÎÊý£º$requestºÍ$next £¬ÆäÖÐ$requestÊÇÇëÇó¹¤¾ß £¬$nextÊÇÏÂÒ»ÆäÖÐÑë¼þ»òÕß¿ØÖÆÆ÷µÄ´¦ÀíÒªÁì ¡£ÔÚhandleÒªÁìÖÐ £¬ÎÒÃÇ¿ÉÒÔ¶Ô$request¹¤¾ß¾ÙÐÐһЩԤ´¦Àí £¬È»ºóʹÓÃreturn $next($request) ŲÓÃÏÂÒ»ÆäÖÐÑë¼þ»òÕß¿ØÖÆÆ÷µÄ´¦ÀíÒªÁì ¡£

×¢²áÖÐÑë¼þ

½¨ÉèºÃÖÐÑë¼þºó £¬ÐèÒªÔÚÓ¦ÓÃÖÐ×¢²á ¡£ÔÚThinkPHP6ÖÐ £¬¿ÉÒÔͨ¹ýÓ¦ÓÃÉèÖá¢Â·Óɽç˵¡¢¿ØÖÆÆ÷×¢½âµÄ·½·¨×¢²áÖÐÑë¼þ ¡£

2.1 Ó¦ÓÃÉèÖÃ

¿ÉÒÔÔÚÓ¦ÓõÄÉèÖÃÎļþconfig/app.phpÖÐÉèÖÃÈ«¾ÖÖÐÑë¼þ»òÕß°´ÐèÉèÖÃÖÐÑë¼þ ¡£

// È«¾ÖÖÐÑë¼þ
'middleware' => [
    appmiddlewareCheckAuth::class,
],

// °´ÐèÉèÖÃÖÐÑë¼þ
'route' => [
    // Route::group Ò²Ö§³Ö
    'blog/detail' => ['appmiddlewareCheckAuth'],
]

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬Ê¹ÓÃmiddlewareÉèÖÃÏî¿ÉÒÔÈ«¾Ö×¢²áÖÐÑë¼þ £¬Ã¿ÆäÖÐÑë¼þµÄÀàÃûÒÔ¶ººÅÍÑÀë ¡£ÔÚrouteÉèÖÃÏîÖÐ £¬¿ÉÒÔÕë¶Ô²î±ðµÄ·ÓÉÖ¸¶¨ÖÐÑë¼þ ¡£

2.2 ·Óɽç˵

¿ÉÒÔÔÚRoute::ruleÒªÁì»òÕßRoute::groupÒªÁìÖÐÖ¸¶¨ÖÐÑë¼þ ¡£

use thinkacadeRoute;

Route::rule('blog/detail', 'blog/detail', 'GET')->middleware('appmiddlewareCheckAuth');

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇÔÚŲÓÃRoute::ruleÒªÁìʱÌí¼ÓÁËmiddlewareÒªÁìÀ´Ö¸¶¨ÖÐÑë¼þ ¡£

2.3 ¿ØÖÆÆ÷×¢½â

¿ÉÒÔÔÚ¿ØÖÆÆ÷×¢½âÖÐÖ¸¶¨ÖÐÑë¼þ ¡£

namespace appcontroller;

/**
 * @middleware(appmiddlewareCheckAuth::class)
 */
class Blog
{
    public function detail()
    {
        // ¿ØÖÆÆ÷µÄ´¦ÀíÂß¼­
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇÔÚ¿ØÖÆÆ÷×¢½âÖÐÌí¼ÓÁËmiddlewareÊôÐÔÀ´Ö¸¶¨ÖÐÑë¼þ ¡£

ÖÐÑë¼þµÄÖ´ÐÐ˳Ðò

ÔÚThinkPHP6ÖÐ £¬ÖÐÑë¼þƾ֤ע²áµÄ˳ÐòÖ´ÐÐ £¬ÏÈ×¢²áÏÈÖ´ÐÐ £¬ºó×¢²áºóÖ´ÐÐ ¡£

ÔÚÓ¦ÓÃÉèÖúͿØÖÆÆ÷×¢½âÖÐ £¬ÎÒÃÇ¿ÉÒÔʹÓÃMiddleware::classÒªÁìÀ´Ö¸¶¨ÖÐÑë¼þµÄÖ´ÐÐ˳Ðò ¡£ÈçÏÂËùʾ£º

// È«¾ÖÖÐÑë¼þƾ֤˳ÐòÖ´ÐÐ
'middleware' => [
    appmiddlewareLog::class,
    appmiddlewareCheckAuth::class,
],

// °´ÐèÉèÖÃÖÐÑë¼þƾ֤˳ÐòÖ´ÐÐ
'route' => [
    'blog/detail' => ['appmiddlewareLog', 'appmiddlewareCheckAuth']
],

// ¿ØÖÆÆ÷×¢½âÖÐÑë¼þƾ֤˳ÐòÖ´ÐÐ
namespace appcontroller;

/**
 * @middleware([appmiddlewareLog::class, appmiddlewareCheckAuth::class])
 */
class Blog
{
    public function detail()
    {
        // ¿ØÖÆÆ÷µÄ´¦ÀíÂß¼­
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇƾ֤ÖÐÑë¼þ×¢²áµÄ˳ÐòÖ¸¶¨ÁËMiddleware::classÒªÁì ¡£

ÖÐÑë¼þµÄ²ÎÊýת´ï

ÖÐÑë¼þÖ®¼ä¿ÉÒÔͨ¹ý²ÎÊýת´ïÀ´¹²ÏíÊý¾Ý ¡£ÔÚhandleÒªÁìÖÐ £¬ÎÒÃÇ¿ÉÒÔÔÚ$request¹¤¾ßÖÐÌí¼ÓÊôÐÔ¡¢ÒªÁì»òÕß²ÎÊý £¬È»ºó½«$request¹¤¾ßת´ï¸øÏÂÒ»ÆäÖÐÑë¼þ»òÕß¿ØÖÆÆ÷ £¬´Ó¶øʵÏÖÊý¾ÝµÄ¹²Ïí ¡£

ÀýÈçÏÂÃæµÄʾÀýÖÐ £¬ÎÒÃÇÔÚµÚÒ»ÆäÖÐÑë¼þÖнç˵ÁËÊôÐÔ$name £¬²¢½«Æäת´ï¸øµÚ¶þÆäÖÐÑë¼þ¼°¿ØÖÆÆ÷ £¬´Ó¶øÈÃËüÃÇ¿ÉÒÔʹÓøÃÊôÐÔ ¡£

<?php

namespace appmiddleware;

class CheckAuth
{
    public function handle($request, Closure $next, $name)
    {
        $request->name = $name;

        return $next($request);
    }
}

class Log
{
    public function handle($request, Closure $next)
    {
        echo 'name:' . $request->name . '<br>';
        return $next($request);
    }
}

namespace appcontroller;

class Blog
{
    public function detail(Request $request)
    {
        echo 'name:' . $request->name . '<br>';
        // ¿ØÖÆÆ÷µÄ´¦ÀíÂß¼­
    }
}

// ·ÓÉÉèÖÃÎļþ
use thinkacadeRoute;

Route::rule('blog/detail', 'blog/detail', 'GET')
->middleware(['appmiddlewareCheckAuth:name', 'appmiddlewareLog']);

µÇ¼ºó¸´ÖÆ

ÉÏÃæµÄ´úÂëÖÐ £¬ÎÒÃÇÔÚCheckAuthÀàµÄhandleÒªÁìÖнç˵ÁË$nameÊôÐÔ £¬²¢½«ÆäÉúÑÄÔÚ$request¹¤¾ßÖÐ ¡£ÔÚLogÀàµÄhandleÒªÁìºÍBlog¿ØÖÆÆ÷ÖÐ £¬ÎÒÃǶ¼¿ÉÒÔͨ¹ý$request¹¤¾ßÀ´»á¼û¸ÃÊôÐÔ ¡£

ÔÚ·Óɽç˵ÖÐ £¬ÎÒÃÇʹÓÃÁËÖÐÑë¼þµÄ²ÎÊýת´ï¹¦Ð§ £¬½«²ÎÊýnameת´ï¸øCheckAuthÖÐÑë¼þ ¡£ÔÚ°´ÐèÖÐÑë¼þµÄÉèÖÃÖÐ £¬Ò²¿ÉÒÔʹÓÃMiddleware::classÒªÁìÀ´Ö¸¶¨ÖÐÑë¼þ²ÎÊý ¡£

Î塢С½á

ÖÐÑë¼þÊÇÒ»ÖÖ³£ÓõÄÇëÇó´¦Àí¹¤¾ß £¬¿ÉÒÔÔÚÇëÇóÇ°ºó¶Ô¿Í»§¶ËÇëÇó¾ÙÐÐÔ¤´¦Àí»òÕߺó´¦Àí ¡£ÔÚThinkPHP6ÖÐ £¬ÖÐÑë¼þÊÇ»ùÓÚPSR-15¹æ·¶Éè¼ÆµÄ £¬Í¨¹ýÖÐÑë¼þ £¬ÎÒÃÇ¿ÉÒÔÀû±ãµØʵÏÖȨÏÞ¿ØÖÆ¡¢ÈÕÖ¾¼Í¼¡¢ÇëÇóÑéÖ¤µÈ¹¦Ð§ ¡£ÖÐÑë¼þƾ֤ע²áµÄ˳ÐòÖ´ÐÐ £¬ÖÐÑë¼þÖ®¼ä¿ÉÒÔͨ¹ý²ÎÊýת´ïÀ´¹²ÏíÊý¾Ý ¡£Í¨¹ýÎÞаʹÓÃÖÐÑë¼þ £¬ÎÒÃÇ¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÇå¾²ÐÔ¡¢¿Éά»¤ÐԺͿÉÀ©Õ¹ÐÔ ¡£

ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖÐʹÓÃÖÐÑë¼þ¾ÙÐÐÇëÇó´¦Àí£¿µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ