chiliec/yii2-vote
Composer 安装命令:
composer require chiliec/yii2-vote
包简介
Provides anonymous voting for any models
关键字:
README 文档
README
A flexible and easy-to-use voting extension for Yii2 that provides anonymous voting functionality for any ActiveRecord models. Support likes/dislikes with aggregate ratings and guest voting capabilities.
Features
- 👍 Like/dislike voting system
- 👤 Guest and authenticated user voting
- 🔄 Optional vote changing
- 📊 Aggregate rating calculations
- 🎨 Customizable widgets
- 🏆 Top-rated models widget
- 🔍 SEO-friendly with rich snippets support
- 💾 Support for MySQL, PostgreSQL, and SQLite
Requirements
- PHP 8 or higher
- Yii2 2.0 or higher
Installation
Step 1: Install via Composer
composer require chiliec/yii2-vote "^4.3"
Step 2: Configure Your Application
Add the following to your application configuration file (e.g., config/main.php):
'bootstrap' => [ 'chiliec\vote\components\VoteBootstrap', ], 'modules' => [ 'vote' => [ 'class' => 'chiliec\vote\Module', // Display messages in popover (default: false) 'popOverEnabled' => true, // Global settings for all models 'allowGuests' => true, // Allow guests to vote (default: true) 'allowChangeVote' => true, // Allow users to change their vote (default: true) // Register your models 'models' => [ // Simple registration \common\models\Post::class, // With string notation 'backend\models\Post', // With custom ID 2 => 'frontend\models\Story', // With model-specific settings (overrides global settings) 3 => [ 'modelName' => \backend\models\Mail::class, 'allowGuests' => false, // Only authenticated users can vote 'allowChangeVote' => false, // Users cannot change their vote ], ], ], ],
Step 3: Run Migrations
Apply the database migrations to create the required tables:
php yii migrate/up --migrationPath=@vendor/chiliec/yii2-vote/migrations
Usage
Basic Vote Widget
Add the vote widget to your view:
<?= \chiliec\vote\widgets\Vote::widget([ 'model' => $model, // Optional: show aggregate rating 'showAggregateRating' => true, ]) ?>
Top Rated Widget
Display a list of top-rated models:
<?= \chiliec\vote\widgets\TopRated::widget([ 'modelName' => \common\models\Post::class, 'title' => 'Top Rated Posts', 'path' => 'site/view', 'limit' => 10, 'titleField' => 'title', ]) ?>
Advanced Configuration
Accessing Vote Data in Your Models
Once configured, your models will have access to voting data through the automatically attached behavior:
$post = Post::findOne(1); echo $post->aggregate->likes; // Number of likes echo $post->aggregate->dislikes; // Number of dislikes echo $post->aggregate->rating; // Calculated rating
Manual Behavior Attachment
If you prefer to manually attach the behavior to specific models:
public function behaviors() { return [ 'vote' => [ 'class' => \chiliec\vote\behaviors\VoteBehavior::class, ], ]; }
Documentation
For detailed documentation, see docs/README.md:
- Migration Guide from 2.* to 3.0
- Manual Behavior Configuration
- Sorting by Rating in Data Providers
- Overriding Views
- Customizing JavaScript Events
- SEO Rich Snippets
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
Contributors
- chiliec - Maintainer
- loveorigami - Ideological inspirer
- fourclub - PK name fix in behavior
- yurkinx - Duplication JS render fix
- n1k88 - German translation
- teranchristian - Add popover to display messages
- Skatox - PostgreSQL support
Alternative Solutions
Looking for other voting solutions for Yii2?
- yii2-vote by hauntd - Vote widgets, like and favorite buttons
- yii2-vote by bigdropinc - Alternative voting implementation
Resources
License
yii2-vote is released under the BSD 3-Clause License. See LICENSE.md for details.
Support
If you find this extension useful, please consider:
- ⭐ Starring the repository
- 🐛 Reporting issues
- 📝 Contributing improvements
- 💬 Sharing your experience
Made with ❤️ by the Yii2 community
chiliec/yii2-vote 适用场景与选型建议
chiliec/yii2-vote 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.24k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2015 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「voting」 「anonymous」 「vote」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chiliec/yii2-vote 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chiliec/yii2-vote 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chiliec/yii2-vote 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ratings Plugin for CakePHP.
Anonymous discussions and replies for logged in users
Create voting campaigns
Let users like things, confirm their likes via email and keep track of the count.
User voting module
Anonymous post support
统计信息
- 总下载量: 6.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 71
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-01-02
