PHP code example of chiliec / yii2-vote

1. Go to this page and download the library: Download chiliec/yii2-vote library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chiliec / yii2-vote example snippets


'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
            ],
        ],
    ],
],

<?= \chiliec\vote\widgets\Vote::widget([
    'model' => $model,
    // Optional: show aggregate rating
    'showAggregateRating' => true,
]) 

<?= \chiliec\vote\widgets\TopRated::widget([
    'modelName' => \common\models\Post::class,
    'title' => 'Top Rated Posts',
    'path' => 'site/view',
    'limit' => 10,
    'titleField' => 'title',
]) 

$post = Post::findOne(1);
echo $post->aggregate->likes;     // Number of likes
echo $post->aggregate->dislikes;  // Number of dislikes
echo $post->aggregate->rating;    // Calculated rating

public function behaviors()
{
    return [
        'vote' => [
            'class' => \chiliec\vote\behaviors\VoteBehavior::class,
        ],
    ];
}
bash
php yii migrate/up --migrationPath=@vendor/chiliec/yii2-vote/migrations