ÔõÑùʹÓÃThinkPHP¹Ø±ÕÕ¾µã
ÔÚijЩÇéÐÎÏ£¬ÎÒÃÇÐèÒª¹Ø±Õ×ðÁú¿Ê±ÍøÕ¾£¬ÀýÈçά»¤¸üлò´¦Öóͷ£¹ÊÕÏ¡£ÔÚÕâÖÖÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔʹÓÃthinkphp¿ò¼ÜÌṩµÄÒ»ÖÖ³ÆΪ”ά»¤Ä£Ê½”µÄÒªÁìÀ´¹Ø±Õ×ðÁú¿Ê±Õ¾µã¡£
ÒÔÏÂÊÇʹÓÃThinkPHP¹Ø±Õ×ðÁú¿Ê±Õ¾µãµÄ°ì·¨£º
°ì·¨1£º½¨ÉèÒ»¸öά»¤ÊÓͼ
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÊÓͼÎļþÀ´ÏÔʾվµãµÄά»¤ÐÅÏ¢¡£ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö°üÀ¨Î¬»¤ÐÅÏ¢µÄHTMLÊÓͼÎļþ£¬²¢½«ÆäÉúÑÄÔÚ×ðÁú¿Ê±Ó¦ÓóÌÐòµÄÊÓͼĿ¼ÖС£ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪmaintenance.htmlµÄÎļþ²¢½«ÆäÉúÑÄÔÚÊÓͼĿ¼ÖеÄadminÎļþ¼ÐÖС£
ÔÚmaintenance.htmlÎļþÖУ¬ÎÒÃÇ¿ÉÒÔдÈëÕ¾µãά»¤ÐÅÏ¢£¬ÀýÈ磺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
nbsp;html> <title>ά»¤Ä£Ê½</title> <h1>Õ¾µãÕýÔÚά»¤ÖУ¬ÇëÉÔºóÔÙÊÔ£¡</h1>
µÇ¼ºó¸´ÖÆ
°ì·¨2£ºÔÚÓ¦ÓóÌÐòÉèÖÃÎļþÖÐÆôÓÃά»¤Ä£Ê½
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª·¿ª×ðÁú¿Ê±Ó¦ÓóÌÐòÉèÖÃÎļþ£¨Î»ÓÚÓ¦ÓóÌÐò¸ùĿ¼ÏµÄconfig.phpÎļþ£©£¬²¢ÆôÓÃά»¤Ä£Ê½¡£ÎÒÃÇ¿ÉÒÔÉèÖÃ’maintenance’Ñ¡ÏÈçÏÂËùʾ£º
return [ // ... 'maintenance' => true, // ... ];
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÉèÖôúÂëÖУ¬ÎÒÃǽ«’maintenance’Ñ¡ÏîÉèÖÃΪtrue£¬Õ⽫ÆôÓÃά»¤Ä£Ê½¡£Ò»µ©ÎÒÃÇ¿ªÆôÁËÕâ¸öÑ¡ÏÈκÎÊÔͼ»á¼ûÕ¾µãµÄÇëÇ󶼽«±»Öض¨Ïòµ½ÎÒÃÇ֮ǰ½ç˵µÄά»¤ÊÓͼ¡£
°ì·¨3£º×Ô½ç˵ά»¤Ò³Ãæ
ÈôÊÇÎÒÃÇÏëÒª×Ô½ç˵Öض¨Ïòµ½Î¬»¤Ò³ÃæµÄÇëÇó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒ»ÆäÖÐÐļþÀ´Ì滻ĬÈÏÐÐΪ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»ÆäÖÐÐļþÎļþ¡£ÎÒÃÇ¿ÉÒÔʹÓÃThinkPHP CLIÏÂÁîÐй¤¾ßÀ´½¨ÉèÒ»ÆäÖÐÐļþ£¬ÈçÏÂËùʾ£º
php think make:middleware Maintenance
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÏÂÁ½¨ÉèÒ»¸öÃûΪMaintenance.phpµÄÖÐÐļþÎļþ£¬¸ÃÎļþ°üÀ¨Ò»¸öhandleÒªÁì¡£ÎÒÃÇ¿ÉÒÔʹÓøÃÒªÁìÀ´×Ô½ç˵×ðÁú¿Ê±Î¬»¤Ò³Ãæ¡£
ÈçÏÂËùʾ£¬ÎÒÃÇ¿ÉÒÔÔÚÖÐÐļþÖÐÖض¨Ïòµ½ÎÒÃÇ֮ǰ½¨ÉèµÄmaintenance.htmlÒ³Ã棺
<?php namespace app\middleware; class Maintenance { public function handle($request, \Closure $next) { // ÅжÏÊÇ·ñ´¦ÓÚά»¤Ä£Ê½ if (config('app.maintenance')) { return redirect('/admin/maintenance.html'); } return $next($request); } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃÇÔÚÖÐÐļþÖнç˵ÁËÒ»¸öhandleÒªÁ죬¸ÃÒªÁìÎüÊÕÇëÇó²¢Ö´ÐÐÒÔÏÂÁ½¸ö²Ù×÷£º
¼ì²éÓ¦ÓóÌÐòÊÇ·ñ´¦ÓÚά»¤Ä£Ê½£»
ÈôÊÇÓ¦ÓóÌÐò´¦ÓÚά»¤Ä£Ê½£¬ÔòÖض¨Ïòµ½ÎÒÃÇ֮ǰ½¨ÉèµÄmaintenance.htmlÒ³Ãæ¡£
°ì·¨4£º×¢²áÖÐÐļþ
×îºó£¬ÎÒÃÇÐèÒª½«×ðÁú¿Ê±ÖÐÐļþ×¢²áµ½Ó¦ÓóÌÐòµÄÈ«¾ÖÖÐÐļþÁбíÖУ¬ÒÔ±ãËü¿ÉÒÔ´¦Öóͷ£ËùÓÐÇëÇó¡£ÎÒÃÇ¿ÉÒÔÔÚÓ¦ÓóÌÐòÉèÖÃÎļþ£¨config.php£©ÖÐÌí¼ÓÒÔÏÂÐУº
return [ // ... 'middleware' => [ \app\middleware\Maintenance::class ], // ... ];
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽ«×ðÁú¿Ê±ÖÐÐļþÌí¼Óµ½’middleware’Êý×éÖС£ÏÖÔÚ£¬µ±ÎÒÃÇʵÑé»á¼ûÕ¾µãʱ£¬ÏµÍ³½«Ê¹ÓÃÎÒÃÇ֮ǰ½¨ÉèµÄÖÐÐļþºÍÊÓͼÎļþ½«×ðÁú¿Ê±ÇëÇóÖض¨Ïòµ½ÎÒÃÇËù½ç˵µÄά»¤Ò³Ãæ¡£
×ܽá
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔõÑùʹÓÃThinkPHP¿ò¼ÜÀ´¹Ø±Õ×ðÁú¿Ê±Õ¾µã¡£ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö¼òÆÓµÄά»¤ÊÓͼ£¬È»ºóÔÚÓ¦ÓóÌÐòÉèÖÃÎļþÖÐÆôÓÃά»¤Ä£Ê½¡£ÈôÊÇÎÒÃÇÒª×Ô½ç˵ά»¤Ò³Ã棬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»ÆäÖÐÐļþ²¢½«Æä×¢²áµ½Ó¦ÓóÌÐòÖС£Õ⽫ʹÎÒÃÇ¿ÉÒÔ×Ô½ç˵ÈκÎÐèҪά»¤´¦Öóͷ£µÄÕ¾µãµÄÐÅÏ¢¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃThinkPHP¹Ø±ÕÕ¾µãµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡