shirase / yii2-vote
点赞和踩模块小部件
v0.1.1
2016-06-03 11:53 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-14 18:04:17 UTC
README
点赞和踩模块小部件
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist shirase/yii2-vote "*"
或者在您的 composer.json
文件的 require 部分添加
"shirase/yii2-vote": "*"
到 require 部分。
使用方法
使用所需类型创建 votes 表,用于用户和模型 ID
CREATE TABLE vote( user_id BIGINT NOT NULL, model_id BIGINT NOT NULL, model VARCHAR(32) NOT NULL, type TINYINT(1) NOT NULL, ip VARCHAR(15) DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (user_id, model_id, model, type) );
或使用在 MODULE/migrations 文件夹中的预定义迁移。
php yii migrate --migrationPath=@vendor/shirase/yii2-vote/migrations
在控制器中包含 VoteAction
public function actions() { return [ 'like'=>[ 'class'=>shirase\vote\actions\VoteAction::className(), 'model'=>shirase\vote\models\Vote::className(), ], 'dislike'=>[ 'class'=>shirase\vote\actions\VoteAction::className(), 'model'=>shirase\vote\models\Vote::className(), 'type'=>-1, ], ]; }
VoteAction 参数
"model" - class name of vote activeRecord. Default value: "shirase\vote\models\Like"
"type" - type of operation that will be executed ( 1 => like, -1 => dislike). Default value: 1
"action" - anonymous function that will be called instead of action.
"allowGuests" - allow action for guest users. Default value: false
默认情况下,投票小部件的客户端脚本发送 "POST",并带有两个参数用于 voteAction
"model" - class name for voted model. By default value encoded with crc32.
"id" - ID for voted model.
默认情况下,VoteAction 从应用程序获取以下参数
"user_id" - get from Yii::$app->user->id
"ip" - get from Yii::$app->request->userIP
使用小部件
扩展安装完成后,只需在您的代码中通过以下方式使用它即可
shirase\vote\widgets\Vote::widget([ 'model' => $model, //*Obligatory parameter. Object for Like/Dislike. 'primaryField' => 'id', //Name of primary key for model 'actionPath'=>'/controllerName/', //Path to controller for like/dislike action. E.g. '/site/' for action '/site/like' 'ajaxOptions'=>[ //ajax attributes 'url'=>"http://custom.url", 'method'=>'POST', 'data'=>['custom'=>'data'], 'dataType'=>'HTML', 'etc'=>'etc', ], 'clientOptions'=>[ //widget attributes 'likeError'=>JsExpression("alert('like success');"); //method on ajax like error 'likeSuccess'=>JsExpression("alert('like error');"); //method on ajax like success 'dislikeSuccess'=>JsExpression("alert('dislike success');"); //method on ajax dislike success 'dislikeError'=>JsExpression("alert('dislike error');"); //method on ajax dislike error ], 'voteModel' => MyLikeModel::className(), //ActiveRecord class of table for storing vote data. 'vote' => $myLikeModel, //Vote table object 'modelField' => 'modelFieldForLikeModel', //Name of field for Vote table in wich store model identifier 'modelIdField' => 'modelIdFieldForLikeModel', //Name of field for Vote table in wich store model primary key 'userIdField' => 'userIdFieldForLikeModel', //Name of field for Vote table in wich store user identifier 'typeField' => 'typeFieldForLikeModel', //Name of field for Vote table in wich store vote type 'guestErrorMessage' => "You are guest, go away!", //Message displaying instead of widget for guest users 'cancelable'=>true, //Is user able to cancel their like ]);