尊龙凯时人生就是搏

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¡¢ÍøÕ¾µØͼ