thinkphp¹³×ÓÒªÁìÔÚÊÂÎñ´¦Öóͷ£ÖÐÔõôӦÓÃ
¹³×ÓÒªÁìÊÇÔ¤ÏȽç˵µÄº¯Êý£¬ÔÚÌض¨ÊÂÎñ±¬·¢Ê±×Ô¶¯Å²Óã¬ÔÚ¿ò¼ÜºÍÓ¦ÓóÌÐòÖÐÆÕ±éÓ¦Óá£ÕâЩÊÂÎñº¸ÇÁËÖîÈçÓ¦ÓóÌÐòÆô¶¯¡¢ÇëÇóµÖ´ï¡¢¿ØÖÆÆ÷ÒªÁìŲÓÃÇ°ºó¡¢Ä£×ÓÊý¾Ý¸üÐÂÇ°ºóµÈÇéÐΡ£¿ª·¢Õß¿ÉÒÔÇáËɵØÌí¼Ó×Ô¼ºµÄÂß¼£¬¶ø²»ÐèÒªÐÞ¸ÄÔÓдúÂ룬ÕâÊÇͨ¹ýʹÓÃÕâЩ¹³×ÓÒªÁìʵÏֵġ£
ÏÂÃæÎÒÃÇÀ´¿´Ò»¸ö¼òÆÓµÄÀý×Ó¡£¼ÙÉèÎÒÃÇÓÐÒ»¸ö¿ØÖÆÆ÷£¬ËüÐèÒªÔÚŲÓÃÒªÁì֮ǰ×öһЩ׼±¸ÊÂÇé¡£ÔÚ¿ØÖÆÆ÷ÀàÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵һ¸öÃûΪ¡°before¡±µÄÒªÁ죬Ȼºó½«Æä×¢²áµ½¡°app_init¡±ÊÂÎñÉÏ£¬Ê¾ÀýÈçÏ£º
<?php namespace app\index\controller; use think\Controller; class Index extends Controller { protected function before() { // ×¼±¸ÊÂÇé } public function index() { // Ö÷ÒªÁì return $this->fetch(); } }
µÇ¼ºó¸´ÖÆ
ÔÚÓ¦ÓóÌÐòÆô¶¯Ê±£¬¿ò¼Ü»á×Ô¶¯Å²ÓÃbeforeÒªÁì¡£ÕâÑù£¬ÎÒÃǾͿÉÒÔÔÚ´¦Öóͷ£ÇëÇó֮ǰִÐÐ×Ô¼ºµÄÂß¼¡£
³ýÁË×¢²á¿ò¼Ü½ç˵µÄÊÂÎñÖ®Í⣬ÎÒÃÇ»¹¿ÉÒÔÔÚÓ¦ÓóÌÐòÖнç˵×Ô¼ºµÄÊÂÎñ¡£¼ÙÉèÎÒÃÇÓÐÒ»¸öÐèÒªÔÚÊÂÎñ´¦Öóͷ£Ê±Ö´ÐеÄÂß¼¡£ÎÒÃÇ¿ÉÒÔ½ç˵һ¸öÃûΪ¡°transaction¡±µÄÊÂÎñ£¬²¢½«Æä×¢²áµ½¡°commit¡±ÊÂÎñÖУ¨µ±ÊÂÎñÌύʱ×Ô¶¯Ö´ÐУ©¡£´úÂëÈçÏÂËùʾ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
<?php namespace app\index\model; use think\Model; class User extends Model { protected function initialize() { $this->registerEvent('transaction', function() { // ÊÂÎñ´¦Öóͷ£Âß¼ }); } }
µÇ¼ºó¸´ÖÆ
initializeÊÇÔÚÄ£×ÓÀà³õʼ»¯Ê±×Ô¶¯Å²ÓõÄÒªÁ죬ÎÒÃÇ¿ÉÒÔÔÚÆäÖнç˵ÊÂÎñ´¦Öóͷ£º¯Êý¡£
ÎÒÃÇÒѾ°ÑÒ»¸öÃûΪ¡°transaction¡±µÄÊÂÎñ½ç˵ºÃ²¢×¢²áµ½ÁËÄ£×ÓÀàÖС£ÎÒÃÇÖ»ÐèÒªÔÚÊÂÎñ´¦Öóͷ£Ê±Å²ÓÃ$this->fireEvent(‘transaction’)¼´¿É´¥·¢¸ÃÊÂÎñ¡£
ÒÔÉϾÍÊÇthinkphp¹³×ÓÒªÁìÔÚÊÂÎñ´¦Öóͷ£ÖÐÔõôӦÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡