无法在yii2中从控制器渲染php变量

时间:2017-10-25 05:35:21

标签: php forms yii2 render

我是yii2的新手。我将两个变量传递到create视图中,该视图部分由_form呈现。但不知何故,我无法传递这些变量,并得到错误。

我做了什么?

在我的action控制器中,我声明了两个global变量

global $the_meter_id;
global $the_meter_msn;

然后在我的其他情况下,我会收到当前的id和当前的serial号码。

else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');

                   $model->attributes=$_REQUEST['MetersInventoryStore'];

                   $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
                   $the_meter_id = $sim->meter_id;
                   $the_meter_msn=$sim->meter_serial;

                   /*print_r($the_meter_msn);
                   print_r($the_meter_id);
                   die();*/

                    return $this->render('create', [
                        'model'=>$model,
                        'the_meter_id'=>$the_meter_id,
                        'the_meter_msn'=>$the_meter_msn

                    ]);
}

然后在我create.php我做了以下

 <?= $this->render('_form', [
    'model' => $model,
    'the_meter_id'=>$the_meter_id,
    'the_meter_msn'=> $the_meter_msn

]) ?>

最后在我的_form

<div class="selcted-meters">
        <input type="hidden" name="selected_meters" value="<?php echo $the_meter_id;?>"/>
        <p><b> <?php echo $the_meter_msn;?></b><br /></p>
        <a href="#" onclick="$(this).closest('selcted-meters').remove()">X</a>    <!--value="'+datum.id+'" />'+datum.meter_serial+'<a onclick="$(this).closest(\'.selcted-meters\').remove()">X</a></div>'-->
    </div>

以上代码是通过以下this解决方案编写的。当我运行此代码时,我在Undefined variable: the_meter_id

中收到错误create.php

我做了什么让事情运转?

我已经尝试了各种可能的方式来渲染/传递我的变量到视图,比如放置变量public然后直接在render中获取值也尝试从中删除变量create.php但仍然遇到同样的错误。

更新代码

我更新了我的If条件并将变量公开

 public $the_meter_id;
 public $the_meter_msn;

在我的action

if($meter_alive=='Yes') {
                    //$model->scenario ='Sim_installed';
                    if (isset($_REQUEST['selected_meters'])) {

                        foreach ($_REQUEST['selected_meters'] as $k => $v) {
                            $m = MetersInventoryStore::findOne($v);
                            $m->load(Yii::$app->request->post());
                            $m->status = MetersInventoryStore::$statuses[1];
                            if ($m->save()) {
                                Meters::change_status($m->meter_id, Meters::$status_titles[2]);
                                \Yii::$app->getSession()->setFlash('success',  '
 <div class="alert alert-success alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Communication is Verified </strong> </div>');
                            }
                        }

                        return $this->render('create', [
                            'model' => $model,
                            $this->the_meter_id,
                            $this->the_meter_msn
                        ]);
                    } else if ($model->load(Yii::$app->request->post()) && $model->save()) {
                        Meters::change_status($model->meter_id, Meters::$status_titles[2]);
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                            $this->the_meter_id,
                            $this->the_meter_msn
                        ]);
                    }
                }else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');
                    $model->attributes=$_REQUEST['MetersInventoryStore'];
                    /*$model->attributes=$_REQUEST['selected_meters'];*/
                    $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
                  $this->the_meter_id =$sim->meter_id;
                  $this->the_meter_msn=$sim->meter_msn;

                   /*print_r($the_meter_msn);
                   print_r($the_meter_id);
                   die();*/
                   /*return $this->render('_form', array('the_meter_id' => $this->the_meter_id, 'the_meter_msn'=> $this->the_meter_msn    ));*/
                    return $this->render('create', [
                        'model'=>$model,
                        $this->the_meter_id,
                        $this->the_meter_msn

                    ]);
}

然后在我的create.php

<?php

 use yii\helpers\Html;


 /* @var $this yii\web\View */
/* @var $model common\models\MetersSimInstallation */

$this->title = 'Create Meters Sim Installation';
$this->params['breadcrumbs'][] = ['label' => 'Meters Sim Installations',   'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

 ?>


 <section class="content-header">
<h1>SIM Installation</h1>
</section>
<section class="content">
<div class="box">
    <div class="box-body">
        <div id="statusMsg" >
                     <?= Yii::$app->session->getFlash('error');?>
                     <?= Yii::$app->session->getFlash('success');?>
         </div>

<?= $this->render('_form', [
    'model' => $model,
     $this->the_meter_id,
     $this->the_meter_msn
]) ?>

    </div>
</div>
 </section>

更新了代码2

if( isset($_REQUEST['selected_meters']))
        {
            $m = MetersInventoryStore::findOne($_REQUEST['selected_meters']);
            $msn = $m->meter_serial; // current selected meter serial number is saved
            $date_time =  str_replace(' ','T',date('Y-m-d H:i:s')); // current date time
            $api_url = 'http://IP:Port/api/meters/GetByMsn/' . $msn . '/' . $date_time ; // my base URL

            $curl = curl_init($api_url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: MY AUTHENTICATION CODE'));

             $curl_response = curl_exec($curl);
             $json=json_decode($curl_response);
             $meter_alive= $json->data->Response;

                if($meter_alive=='Yes') {
                    //$model->scenario ='Sim_installed';
                    if (isset($_REQUEST['selected_meters'])) {

                        foreach ($_REQUEST['selected_meters'] as $k => $v) {
                            $m = MetersInventoryStore::findOne($v);
                            $m->load(Yii::$app->request->post());
                            $m->status = MetersInventoryStore::$statuses[1];
                            if ($m->save()) {
                                Meters::change_status($m->meter_id, Meters::$status_titles[2]);
                                \Yii::$app->getSession()->setFlash('success',  '
 <div class="alert alert-success alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Communication is Verified </strong> </div>');
                            }
                        }

                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    } else if ($model->load(Yii::$app->request->post()) && $model->save()) {
                        Meters::change_status($model->meter_id, Meters::$status_titles[2]);
                        return $this->redirect(['view', 'id' => $model->id]);
                    } else {
                        return $this->render('create', [
                            'model' => $model,
                        ]);
                    }
                }else{//$meter_alive condition else
                    \Yii::$app->getSession()->setFlash('error',  '
 <div class="alert alert-error alert-dismissable">
 <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
 <strong>Validation error! </strong> Try GPRS Communication Again.</div>');
                    $model->attributes=$_REQUEST['MetersInventoryStore'];

                    $sim = MetersInventoryStore::findOne($_REQUEST['selected_meters']);



                    return $this->render('create', [
                        'model'=>$model,
                        'the_meter_id' => $this->the_meter_id=$sim->meter_id,
                        'the_meter_msn' => $this->the_meter_msn=$sim->meter_msn
                    ]);


                }
        }
             return $this->render('create', [
                'model' => $model,

            ]);

运行代码后,我在Getting unknown property: yii\web\View::the_meter_id

中收到错误create.php

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:1)

在控制器使用..

return $this->render('create', [
       'model'=>$model,
       'the_meter_id'=>$the_meter_id,
       'the_meter_msn'=>$the_meter_msn
]);

愚蠢的错误.. 使用

<?= $this->render('_form', [
    'model' => $model,
    'the_meter_id'=>$the_meter_id,
    'the_meter_msn'=>$the_meter_msn
]) ?>

而不是

<?= $this->render('_form', [
    'model' => $model,
     $this->the_meter_id,
     $this->the_meter_msn
]) ?>

create.php