首页 技术 正文
技术 2022年11月18日
0 收藏 648 点赞 3,507 浏览 2679 个字

1、注册事件以及监听器

首先我们需要在 app/Providers/目录下的EventServiceProvider.php中注册事件监听器映射关系,如下:
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
//上面是框架自带
'App\Events\BlogView' => [
'App\Listeners\BlogViewListener',
],
];

  

然后项目根目录下执行如下命令
php artisan event:generate
该命令完成后,会分别自动在 app/Events和app/Listensers目录下生成 BlogView.php和BlogViewListener.php文件。
2、定义事件
app/Events/目录下的BlogView.php中定义事件,在__construct()的参数填写我们需要操作的类。如下:
<?phpnamespace App\Events;use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Model\Admin\ArticleModel;class BlogView
{
use Dispatchable, InteractsWithSockets, SerializesModels; /**
* Create a new event instance.
*
* @return void
*/
public function __construct(ArticleModel $article)
{
$this->article = $article;
} /**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}

 3、定义监听

app/Listeners/目录下的BlogViewListener.php中,在handle()的处理业务逻辑。如下:
<?phpnamespace App\Listeners;use App\Events\BlogView;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;class BlogViewListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
} /**
* Handle the event.
*
* @param BlogView $event
* @return void
*/
public function handle(BlogView $event)
{
$event->article->where('art_id', '=', $event->article->art_id)->increment('clicks'); //点击自增//保存数据库
}
}

 4、触发事件

<?phpnamespace App\Http\Controllers\Home;use App\Events\BlogView;
use Illuminate\Support\Facades\Event;
use App\Model\Admin\ArticleModel;
use App\Model\Admin\CategoryModel;
use Illuminate\Http\Request;class IndexController extends CommonController
{ public function article(Request $request, $id){
$articleModel = new ArticleModel();
// $articleModel->where('art_id', '=', $id)->increment('clicks'); //点击自增
$data = $articleModel->findOrFail($id);
$categoryModel = new CategoryModel();
$cate = $categoryModel->parentTree($data->cate_id); //获取上级分类
$pre = $articleModel->where([['cate_id','=',$data->cate_id],['art_id','<',$id]])->first(); //获取上一篇文章
$next = $articleModel->where([['cate_id','=',$data->cate_id],['art_id','>',$id]])->first();
$relation = $articleModel->where('cate_id', '=', $data->cate_id)->get(); //获取相关文章
Event::dispatch(new BlogView($data)); //通过查看源码,Event::fire 更改为Event::dispatch
// event(new BlogView($data));
return View('home.new',compact('data','cate','pre', 'next', 'relation'));
}}

  

 其他关于事件的操作,如队列。

链接:https://laravelacademy.org/post/19508.html

 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295