×ðÁú¿­Ê±

ThinkphpÊÂÎñ»úÖÆÔõÑùʵÏÖ

ÊÂÎñ»úÖƵÄʵÏÖÓÐÁ½ÖÖ;¾¶£ºÍ¨¹ý¼àÌý¡¢Í¨¹ý¶©ÔÄ

Ò»¡¢Í¨¹ý¼àÌý

1¡¢½¨Éè¼àÌýÀࣺÔÚÏÂÁîÐÐģʽϽøÈë¿ò¼Ü¸ùĿ¼ִÐÐ

php think make:listener

ÀýÈ磺

php think make:listener UserListener

Ö´ÐÐÖ®ºó½«ÔÚ\app\listener\ÏÂÌìÉúUserListenerÕâ¸öÀà ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

2¡¢ÉèÖüàÌý£ºÔÚ\app\event.phpÕâ¸öÎļþµÄlistenÊý×éÖÐÉèÖÃUserListenerÕâ¸öÀ࣬ÈçÏ£º

‘listen’    => [

    ‘testEvent’ => [‘app\listener\UserListener’]

],

3¡¢´¥·¢¼àÌý£ºÔÚÄãÐèÒª´¥·¢¼àÌýµÄµØ·½¼ÓÈëÈçÏ´úÂë

event('testEvent');

µÇ¼ºó¸´ÖÆ

×¢ÖØ£ºÕâÀïµÄ²ÎÊýtestEvent¼´ÎªÊÂÎñÃû³Æ£¬¿ÉËæ±ã½ç˵£¬µ«±ØÐèÒªÓëÔÚevent.phpÎļþÀïÉèÖõļüÃûÒ»Ö ¡£

´¥·¢¼àÌýʱÄã¿ÉÒÔ´øÉϵڶþ¸ö²ÎÊý ¡£Í¨¹ýµÚ¶þ¸ö²ÎÊýÄã¿ÉÒÔ°ÑÈκÎÊý¾Ýת´ïµ½UserListenerÕâ¸öÀàÖУ¬Ò²¾ÍÊÇhandleÒªÁìµÄ²ÎÊý£¨²»Ã÷ȷɶÒâ˼µÄÍŽáÏÂÃæµÄÍêÕû´úÂëÃ÷È·£© ¡£

4¡¢´¦Öóͷ£¼àÌýÂß¼­£ºÔÚUserListenerÀàµÄhandleÒªÁìÖÐÍê³ÉÓªÒµÂß¼­

ÍêÕû´úÂëÈçÏÂ

Other£ºÒ»¸öÓÃÀ´²âÊÔµÄÀà

<?php
namespace app\controller;
class Other
{
    public function hello()
    {
        echo 'hello function';
    }
}

µÇ¼ºó¸´ÖÆ

Index£º´¥·¢¼àÌýµÄÀà

  
  
   µÇ¼ºó¸´ÖÆ
  

UserListener£ºÊÂÎñ¼àÌýÀà

<?php
namespace app\listener;
class UserListener
{
    /**
     * ÊÂÎñ¼àÌý´¦Öóͷ£
     *
     * @return mixed
     */
    public function handle($event)
    {
        //µ±Å²ÓÃIndexÀàµÄtest1ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪnull
        var_dump($event);
        //µ±Å²ÓÃIndexÀàµÄtest2ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪ×Ö·û´®hello
        var_dump($event);
        //µ±Å²ÓÃIndexÀàµÄtest3ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪIndexÀàµÄʵÀý
        //¿ÉÒÔ»á¼ûIndexÀàµÄname±äÁ¿
        echo $event->name;
        //µ±Å²ÓÃIndexÀàµÄtest4ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪOtherÀàµÄʵÀý
        //¿ÉÒÔŲÓÃOtherÀàµÄhelloÒªÁì
        echo $event->hello();
    }
}

µÇ¼ºó¸´ÖÆ

¶þ¡¢Í¨¹ý¶©ÔÄ

1¡¢½¨É趩ÔÄÀà

ÔÚÏÂÁîÐÐģʽϽøÈë¿ò¼Ü¸ùĿ¼ִÐÐ

php think make:subscribe

ÀýÈ磺

php think make:subscribe UserSubscribe

Ö´ÐÐÖ®ºó½«ÔÚ \app\subscribe\ÏÂÌìÉúUserSubscribeÕâ¸öÀà ¡£

2¡¢ÉèÖüàÌý

ÔÚ \app\event.phpÕâ¸öÎļþµÄsubscribeÊý×éÖÐÉèÖÃUserSubscribeÕâ¸öÀ࣬ÈçÏ£º

‘subscribe’ => [‘app\subscribe\UserSubscribe’],

×¢ÖØ£ºÎªÁ˲âÊÔЧ¹û£¬Òª°ÑÉÏÃæÉèÖõÄUserListener´ÓlistenÊý×éɾ³ýµô

3¡¢´¥·¢¼àÌý

¸úµÚÒ»ÖÖ;¾¶Ò»Ñù

4¡¢´¦Öóͷ£¼àÌýÂß¼­

´¦Öóͷ£¼àÌýµÄÂß¼­ÓÖÓÐÁ½ÖÖÒªÁ죬ÎÒ³Æ֮Ϊ×Ô¶¯°ó¶¨ºÍÊÖ¶¯°ó¶¨ ¡£ÊÖ¶¯°ó¶¨ºÍ×Ô¶¯°ó¶¨ÊÇ»¥³âµÄ£¬Ö»ÄܶþÑ¡Ò» ¡£

4.1 ×Ô¶¯°ó¶¨

Íê³ÉÓªÒµÂß¼­µÄontestEventÒªÁìÓ¦¸Ã±»Ìí¼Óµ½UserSubscribeÀàÖÐ ¡£Ìí¼ÓµÄÒªÁìÃû²»¿ÉËæÒâ½ç˵£¬¹æÔòÊÇÊÂÎñÃû³ÆÇ°Ãæ¼ÓÉÏon ¡£ÓÉÓÚÎÒÃÇÔÚevent.phpÀï½ç˵µÄÊÂÎñÃû³ÆÊÇtestEvent£¬ÒÔÊÇÒªÁìÃû¾ÍÊÇontestEvent£¨×¢ÖؾÞϸд£© ¡£¼àÌýÒªÁì½ç˵֮ºó¾Í×Ô¶¯ÓëÊÂÎñ°ó¶¨£¬ÒÔÊÇÎÒ³Æ֮Ϊ×Ô¶¯°ó¶¨ ¡£

ÍêÕû´úÂëÈçÏÂ

OtherºÍIndexÀàµÄ´úÂë¸úµÚÒ»ÖÖ;¾¶Ò»Ñù

UserSubscribe£ºÊÂÎñ¶©ÔÄÀà

<?php
namespace app\subscribe;
class UserSubscribe
{
    public function ontestEvent($user)
    {
        //µ±Å²ÓÃIndexÀàµÄtest1ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪnull
        var_dump($user);
        //µ±Å²ÓÃIndexÀàµÄtest2ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪ×Ö·û´®hello
        var_dump($user);
        //µ±Å²ÓÃIndexÀàµÄtest3ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪIndexÀàµÄʵÀý
        //¿ÉÒÔ»á¼ûIndexÀàµÄname±äÁ¿
        echo $user->name;
        //µ±Å²ÓÃIndexÀàµÄtest4ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$eventΪOtherÀàµÄʵÀý
        //¿ÉÒÔŲÓÃOtherÀàµÄhelloÒªÁì
        echo $user->hello();
    }
}

µÇ¼ºó¸´ÖÆ

4.2 ÊÖ¶¯°ó¶¨

Ê×ÏÈ£¬½¨ÉèÊÂÎñÀ࣬ÈÏÕæ´¦Öóͷ£ÏêϸµÄÓªÒµÂß¼­ ¡£ÔÚÏÂÁîÐÐģʽϽøÈë¿ò¼Ü¸ùĿ¼ִÐУº

php think make:event

ÀýÈ磺

php think make:event UserEvent

Ö´ÐÐÖ®ºó½«ÔÚ \app\event\ÏÂÌìÉúUserEventÕâ¸öÀà ¡£

ÔÙÔÚ¸ÃÀàÖнç˵һ¸öhandleÒªÁ죬¸ÃÒªÁìÓÐÒ»¸ö´«Èë²ÎÊý ¡£ÒªÁìÃûºÍ´«ÈëµÄ²ÎÊýÃû¿ÉÒÔí§Òâ½ç˵ ¡£ÏêϸµÄÓªÒµÂß¼­¾Í¿ÉÒÔ·ÅÔÚhandleÒªÁìÄÚÀï´¦Öóͷ£ ¡£

½ÓÏÂÀ´£¬ÔÚUserSubscribeÀàÄÚÀïÐÂÔösubscribeÒªÁ죬¸ÃÒªÁìÐèÒªÎüÊÕÒ»¸öEvent¹¤¾ß×÷Ϊ²ÎÊý ¡£½Ó×Å£¬Ôڸú¯ÊýÀォÊÂÎñ¼àÌý°ó¶¨µ½UserEventÀàµÄhandleÒªÁìÉÏ ¡£ÈçÏ£º

$event->listen('testEvent', [app('app\event\UserEvent'), 'handle']);

µÇ¼ºó¸´ÖÆ

ÍêÕû´úÂëÈçÏÂ

OtherºÍIndexÀàµÄ´úÂë¸úµÚÒ»ÖÖ;¾¶Ò»Ñù

UserSubscribe£ºÊÂÎñ¶©ÔÄÀà

  
  
   µÇ¼ºó¸´ÖÆ
  

UserEvent£ºÊÂÎñ´¦Öóͷ£Àà

<?php
declare (strict_types = 1);
namespace app\event;
class UserEvent
{
    public function handle($user)
    {
        //µ±Å²ÓÃIndexÀàµÄtest1ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$userΪnull
        var_dump($user);
        //µ±Å²ÓÃIndexÀàµÄtest2ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$userΪ×Ö·û´®hello
        var_dump($user);
        //µ±Å²ÓÃIndexÀàµÄtest3ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$userΪIndexÀàµÄʵÀý
        //¿ÉÒÔ»á¼ûIndexÀàµÄname±äÁ¿
        echo $user->name;
        //µ±Å²ÓÃIndexÀàµÄtest4ÒªÁìʱ¸Ã´úÂëÓÐÓã¬$userΪOtherÀàµÄʵÀý
        //¿ÉÒÔŲÓÃOtherÀàµÄhelloÒªÁì
        echo $user->hello();
    }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉϾÍÊÇThinkphpÊÂÎñ»úÖÆÔõÑùʵÏÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±ÊµÊ±ÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
¡¾ÍøÕ¾µØͼ¡¿¡¾sitemap¡¿