尊龙凯时人生就是博

ÔõÑùÔÚ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
sitemap¡¢ÍøÕ¾µØͼ