Answer the question
In order to leave comments, you need to log in
How does the LikeButton widget work?
I picked it up, a good widget for the like button on yii2, but I don’t understand how these lines work if there is no such directory
$url = '/like/add';
$url = '/like/remove';
here is the code itself:
namespace yii\grid;
use yii;
use yii\helpers\Html;
use yii\helpers\Url;
use common\models\Like;
class LikeButton extends \yii\base\Widget
{
public $text = NULL;
public $model = NULL;
public $cssClass = NULL;
public $cssClassInList = NULL;
public $htmlTag = 'span';
public function init()
{
parent::init();
\frontend\assets\AppAsset::register($this->getView());
if ($this->text === NULL) {
$this->text = 'Мне нравится';
}
if ($this->cssClass === NULL) {
$this->cssClass = 'btn btn-default';
}
return true;
}
public function run()
{
$action = 'add';
$url = '/like/add';
$model = $this->model;
$totalCount = Like::find()->where([
'model' => $model::className(),
'item_id' => $model->id,
])->count();
$textTemplate = $this->text . ' ' .$totalCount;
$currentUserId = \Yii::$app->user->getId();
if ($currentUserId === $model->id) {
return Html::tag($this->htmlTag, $textTemplate, [
'class' => 'btn btn-default',
'data-role' => 'hal_like_button_my_likes',
]);
}
$elementModel = Like::find()->where([
'user_id' => $currentUserId,
'model' => $model::className(),
'item_id' => $model->id,
])->one();
if ($elementModel) {
$textTemplate = $this->text . ' ' . $totalCount;
$this->cssClass .= ' '.$this->cssClassInList;
$action = 'remove';
$url = '/like/remove';
}
return Html::tag($this->htmlTag, $textTemplate, [
'class' => $this->cssClass,
'data-role' => 'hal_like_button',
'data-url' => Url::toRoute($url),
'data-action' => $action,
'data-item-id' => $model->id,
'data-model' => $model::className()
]);
}
}
Answer the question
In order to leave comments, you need to log in
I understand you are using this module . In the controllers folder, and specifically in ElementController, there are actions that you are talking
about Routing in modules
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question