Pjax c параметром enablePushState и сессии(Session) между запросами (Yii2)

← вернуться к статье

Controller

<?php
    public function actionPjaxExample6($vote = null)
    {
        $votes = Yii::$app->session->get('votes', 0);
        if ($vote === 'up') {
            Yii::$app->session->set('votes', ++$votes);
        } elseif ($vote === 'down') {
            Yii::$app->session->set('votes', --$votes);
        }
        return $this->render('pjax_example_6');
    }
?>

View

<?php Pjax::begin(['enablePushState' => false]); ?>
    <?= Html::a('', ['/example/pjax/pjax-example-6', 'vote' => 'up'], ['class' => 'btn btn-lg btn-warning glyphicon glyphicon-arrow-up']) ?>
    <?= Html::a('', ['/example/pjax/pjax-example-6', 'vote' => 'down'], ['class' => 'btn btn-lg btn-warning glyphicon glyphicon-arrow-down']) ?>
    <p><?= Yii::$app->session->get('votes', 0) ?></p>
<?php Pjax::end(); ?>

<?php Pjax::begin(['enablePushState' => true]); ?>
    <?= Html::a('', ['/example/pjax/pjax-example-6', 'vote' => 'up'], ['class' => 'btn btn-lg btn-warning glyphicon glyphicon-arrow-up']) ?>
    <?= Html::a('', ['/example/pjax/pjax-example-6', 'vote' => 'down'], ['class' => 'btn btn-lg btn-warning glyphicon glyphicon-arrow-down']) ?>
    <p><?= Yii::$app->session->get('votes', 0) ?></p>
<?php Pjax::end(); ?>

Пример с отключенным PushState

0

Пример с включенным PushState

0

comments powered by HyperComments