PHP code example of indifferend / yii2-comments

1. Go to this page and download the library: Download indifferend/yii2-comments 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/ */

    

indifferend / yii2-comments example snippets


php yii migrate --migrationPath=@vendor/indifferend/yii2-comments/migrations

'modules' => [
    'comment' => [
        'class' => 'indifferend\comments\Module',
    ],
]


public function getAvatar()
{
    // your custom code
}

public function getUsername()
{
    // your custom code
}


// the model to which are added comments, for example:
$model = Post::find()->where(['title' => 'some post title'])->one();

 echo \indifferend\comments\widgets\Comment::widget([
    'model' => $model,
]); 

 echo \indifferend\comments\widgets\Comment::widget([
    'model' => $model,
    'commentView' => '@app/views/site/comments/index' // path to your template
]); 

 echo \indifferend\comments\widgets\Comment::widget([
        'model' => $model,
]); 

 echo \indifferend\comments\widgets\Comment::widget([
      'model' => $model,
      'dataProviderConfig' => [
          'pagination' => [
              'pageSize' => 10
          ],
      ]
]); 

 echo \indifferend\comments\widgets\Comment::widget([
      'model' => $model,
      'relatedTo' => 'User ' . \Yii::$app->user->identity->username . ' commented on the page ' . \yii\helpers\Url::current(),
      'maxLevel' => 2,
      'dataProviderConfig' => [
          'pagination' => [
              'pageSize' => 10
          ],
      ],
      'listViewConfig' => [
          'emptyText' => Yii::t('app', 'No comments found.'),
      ],
]); 

'modules' => [
    'comment' => [
        'class' => 'indifferend\comments\Module',
        'controllerMap' => [
            'default' => [
                'class' => 'indifferend\comments\controllers\DefaultController',
                'on beforeCreate' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on afterCreate' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on beforeDelete' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
                'on afterDelete' => function ($event) {
                    $event->getCommentModel();
                    // your custom code
                },
            ]
        ]
    ]
]

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'indifferend.comments' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@indifferend/comments/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];

php composer.phar