ÔõÑùÔÚ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ÖÐʹÓÃÖÐÑë¼þ¾ÙÐÐÇëÇó´¦Àí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡