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.
- π 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
- PHP 8 or higher
- Yii2 2.0 or higher
composer require chiliec/yii2-vote "^4.3"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
],
],
],
],Apply the database migrations to create the required tables:
php yii migrate/up --migrationPath=@vendor/chiliec/yii2-vote/migrationsAdd the vote widget to your view:
<?= \chiliec\vote\widgets\Vote::widget([
'model' => $model,
// Optional: show aggregate rating
'showAggregateRating' => true,
]) ?>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',
]) ?>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 ratingIf you prefer to manually attach the behavior to specific models:
public function behaviors()
{
return [
'vote' => [
'class' => \chiliec\vote\behaviors\VoteBehavior::class,
],
];
}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
Contributions are welcome! Please see CONTRIBUTING.md for details.
- 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
Looking for other voting solutions for Yii2?
- yii2-vote by hauntd - Vote widgets, like and favorite buttons
- yii2-vote by bigdropinc - Alternative voting implementation
yii2-vote is released under the BSD 3-Clause License. See LICENSE.md for details.
If you find this extension useful, please consider:
- β Starring the repository
- π Reporting issues
- π Contributing improvements
- π¬ Sharing your experience
Made with β€οΈ by the Yii2 community
