Yii2:传递变量以查看具有不同Action的视图

时间:2016-11-01 18:07:48

标签: php model-view-controller yii2 session-variables

我在一个控制器site中有两个操作,如下所示:现在我想将操作$count中的变量check传递给actionDomain

public function actionDomain()
    {

      $model = new DomainCheckerForm();
      return $this->render('domainChecker', [
            'model' => $model,

        ]);

    }
     public function actionCheck()

     {          
        $model = new DomainCheckerForm();
        if ($model->load(Yii::$app->request->post())) {          
        $session = Yii::$app->session;
        $session->open();
        if(!isset($_SESSION['count']))
        {
           $_SESSION['count'] = 1;
           $_SESSION['first'] = time();
        }
        else
        {
           // Increase the Count
           $_SESSION['count']++;
        }
$count=($_SESSION['count']);    
   // var_dump($count);exit;

我尝试了多种方式,但没有取得任何成功,例如:

echo $this->render('domainChecker', [
    'count' => $count

]);

Yii::$app->runAction('domain', ['count'=>$count]);

但似乎没有任何效果。

更新:domainChecker.php内容

<head>
<style>
.loader {
    text-align: center;    
}
.loader span {
    display: inline-block;
    vertical-align: middle;
    width: 10px;
    height: 10px;
    margin: 50px auto;
    background: black;
    border-radius: 50px;
    -webkit-animation: loader 0.9s infinite alternate;
    -moz-animation: loader 0.9s infinite alternate;
}
.loader span:nth-of-type(2) {
    -webkit-animation-delay: 0.3s;
    -moz-animation-delay: 0.3s;
}
.loader span:nth-of-type(3) {
    -webkit-animation-delay: 0.6s;
    -moz-animation-delay: 0.6s;
}
@-webkit-keyframes loader {
  0% {
    width: 10px;
    height: 10px;
    opacity: 0.9;
    -webkit-transform: translateY(0);
  }
  100% {
    width: 24px;
    height: 24px;
    opacity: 0.1;
    -webkit-transform: translateY(-21px);
  }
}
@-moz-keyframes loader {
  0% {
    width: 10px;
    height: 10px;
    opacity: 0.9;
    -moz-transform: translateY(0);
  }
  100% {
    width: 24px;
    height: 24px;
    opacity: 0.1;
    -moz-transform: translateY(-21px);
  }
}
    </style>


<?php
$script = <<< JS
jQuery(document).ready(function($) {
      $('.loader').hide();
$("#domain-check").submit(function (event) {

    /* Stop form from submitting normally */
    event.preventDefault();


    /* Clear result div*/
    $("#result").html('');

    /* Get some values from elements on the page: */
    var values = $(this).serialize();
    $('.loader').show();
    /* Send the data using post and put the results in a div */

    $.ajax({
        url: "?r=site/check",
        type: "post",
        data: values,
        success: function (response) {
           $('.loader').hide();
            $('#result').html(response);
        }
    });
});
});
JS;
$this->registerJs($script);
?>


</head>



<?php


/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\ContactForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
//use yii\web\Session;



$this->title = 'Check Domain';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        If you have business inquiries or other questions, please fill out the Contact form to contact us. Thank you.
    </p>

    <div class="row">

            <div class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1">
            <?php $form = ActiveForm::begin(['id' => 'domain-check','enableAjaxValidation'   => true,
    'enableClientValidation' => false,

        'fieldConfig' => [
            'options' => [
                'tag' => 'span',
            ],

        ],
    ]); ?>


              <?=  $form->field($model, 'domain',[
                  'template' =>
                  '<div class="input-group ">
                      {input}
                  {error}','inputOptions' => [
            'placeholder' => 'i.e. yourbusinessname.com',
            'class'=>'form-control',
        ]])
    ?>

               <span class="input-group-btn">
                   <input class="btn btn-success " value="Search" type="submit"></span></div>

                <?php


                echo $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
                ]) ?>
 <?php echo $count; ?>


            <?php ActiveForm::end(); ?>
        </div>
    </div>

</div>

<div class="loader">
    <span></span>
    <span></span>
    <span></span>
</div> 
<div id="result" class="row"></div>

1 个答案:

答案 0 :(得分:0)

您应该使用return而不是echo

return $this->render('domainChecker', [
    'count' => $count
]);

您可以使用

从其他操作调用操作

例如

 $this->redirect(array('your-controller/your-action', 'var1'=>$var1, 'var2'=>$var2));

在你的情况下,你应该添加适当的参数动作定义

public function actionYourAction($var1, $var2)
{
     ..... 
}