@good_beginer

Как работает виджет LikeButton?

Расковырял, хороший виджет для кнопки лайка на yii2 но никак не пойму как работаю эти строки если такого директория в принципе нету
$url = '/like/add';
$url = '/like/remove';

вот сам код:
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()
        ]);
    }
}
  • Вопрос задан
  • 81 просмотр
Решения вопроса 1
uDenX
@uDenX
PHP Developer
Я так понимаю Вы используете этот модуль. В папке controllers, а конкретно в ElementController есть действия, о которых вы говорите

Роутинг в модулях
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы