balajidharma/laravel-viewable
Composer 安装命令:
composer require balajidharma/laravel-viewable
包简介
Track Page views for your Laravel projects
README 文档
README
Track Page views for your Laravel projects.
Credits
This package builds upon the work done in cyrildewit/eloquent-viewable and has been modified to suit specific needs. We are grateful to the original author and contributors for their work.
Features
- Track page views for Laravel Eloquent models
- Configure unique views by IP, session, and authenticated users
- Bot detection and filtering
- Support for DNT (Do Not Track) header
- Configurable view counting and storage
Table of Contents
Installation
- Install the package via composer
composer require balajidharma/laravel-viewable
- Publish the migration with
php artisan vendor:publish --provider="BalajiDharma\LaravelViewable\ViewableServiceProvider" --tag="migrations"
- Run the migration
php artisan migrate
- To Publish the config/viewable.php config file with
php artisan vendor:publish --provider="BalajiDharma\LaravelViewable\ViewableServiceProvider" --tag="config"
- Preparing your model To associate views with a model, the model must implement the HasViewable trait:
<?php namespace BalajiDharma\LaravelForum\Models; use BalajiDharma\LaravelViewable\Traits\HasViewable; use Illuminate\Database\Eloquent\Model; class Thread extends Model { use HasViewable;
- Recording views To make a view record, you can call the record method.
public function show(Thread $thread) { $thread->record();
Laravel Viewable Configuration
This document describes all configuration options available in the viewable.php config file.
Configuration Options
Models
'models' => [ 'viewable' => BalajiDharma\LaravelViewable\Models\Viewable::class, ],
Defines the model class used for tracking views. You can override this with your own model class if needed.
'table_names' => [ 'viewable' => 'views', ],
Specifies the database table name used for storing views. Default is 'views'.
Bot Detection
'ignore_bots' => true,
true: Ignores views from bots and crawlersfalse: Records views from all visitors including bots- Default:
true
Do Not Track (DNT) Header
'honor_dnt' => false,
true: Respects the Do Not Track (DNT) header from browsersfalse: Records views regardless of DNT header- Default:
false
Unique View Settings
'unique_ip' => true, 'unique_session' => true, 'unique_viewer' => true,
Controls how unique views are tracked:
unique_ip: Records only one view per IP addressunique_session: Records only one view per sessionunique_viewer: Records only one view per authenticated user- Default: All set to
true
Model View Counter
'increment_model_view_count' => false, 'increment_model_column_name' => 'view_count',
increment_model_view_count: Enable/disable automatic view count increment on the modelincrement_model_column_name: Specifies the column name for storing view count- Default: Counter disabled, column name set to 'view_count'
IP Address Filtering
'ignored_ip_addresses' => [ //'127.0.0.1', ],
- Array of IP addresses to ignore when recording views
- Views from these IPs will not be recorded
- Default: Empty array (no IPs ignored)
Control configuration on model
You able to control all the configurtion on model, by adding below properties
<?php namespace BalajiDharma\LaravelForum\Models; use BalajiDharma\LaravelViewable\Traits\HasViewable; use Illuminate\Database\Eloquent\Model; class Thread extends Model { use HasViewable; protected $ignore_bots = true; protected $honor_dnt = true; protected $unique_session = false; protected $unique_ip = false; protected $unique_viewer = false; protected $increment_model_view_count = true; protected $increment_model_column_name = 'view_count'; protected $ignored_ip_addresses = [ '127.0.0.1', '0.0.0.0' ]
Demo
The "Basic Laravel Admin Penel" starter kit come with Laravel Viewable
balajidharma/laravel-viewable 适用场景与选型建议
balajidharma/laravel-viewable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「viewable」 「laravel-framework」 「laravel-views」 「laravel-viewable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 balajidharma/laravel-viewable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 balajidharma/laravel-viewable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 balajidharma/laravel-viewable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel package that allows you to associate views with Eloquent models
DataExtension that easily allows a DataObject to be viewed like a Page
A flexible attribute management system for Laravel models
Reaction management system for Laravel models
Laravel form builder
Laravel CRUD - Build List and Detail View
统计信息
- 总下载量: 5.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 17
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-09