码迷,mamicode.com
首页 > 其他好文 > 详细

laravel event

时间:2015-09-14 13:53:31      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:

  1. EventServiceProvider
    namespace App\Providers;
    
    class EventServiceProvider extends ServiceProvider
    {
    
        protected $listen = [
            ‘App\Events\UserHasRegistered‘ => [   //修改event
                ‘App\Listeners\Email@welcome‘,   //修改listener,Email是类名,welcome是函数名
            ],
        ];

     

  2. 创建event和listener
    E:\wamp\www\laravel>php artisan  event:generate

    技术分享

  3. 路由
    Route::get(broadcast, function () {
        event(new App\Events\UserHasRegistered(Jeffrey Way));
    });
  4. 在event中接受name参数
    namespace App\Events;
    
    class UserHasRegistered extends Event
    {
        use SerializesModels;
    
        public $name;
    
        public function __construct($name)
        {
            $this->name = $name;
        }

     

  5. 在listener中处理
    namespace App\Listeners;
    
    class Email
    {
         public function __construct()
        {
            //
        }
    
        public function handle(UserHasRegistered $event)
        {
            var_dump(‘The user ‘.$event->name . ‘ has registered. Fire off an email.‘); //可以调用到name参数
        }

     

  6. 在页面进行测试
  7. 技术分享
  8. 啊啊啊啊

laravel event

标签:

原文地址:http://www.cnblogs.com/fenle/p/4806819.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!