ÔõÑùʹÓÃ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¹Ø±ÕÕ¾µãµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡