Unable to render php variables from controller to view in yii2

梦想的初衷 提交于 2019-12-07 18:05:12

问题


I am new to yii2. I am passing two variables into my create view which is partially rendered from _form. But somehow I am unable to pass these variables, and getting error.

What i have done?

In my action controller I have declared two global variables

global $the_meter_id;
global $the_meter_msn;

Then in my else case condition I am getting the current id and current serial number.

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

                    ]);
}

Then in my create.php I have done the following

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

]) ?>

And finally in my _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>

The above code is written by following this solution. When I am running this code I am getting the error Undefined variable: the_meter_id in create.php

What I have done to make things run?

I have tried each and every possible way to render/pass my variables to the view, like putting the variables public and then getting the values direct in the render also tried to remove the variables from create.php but still getting same error.

Updated Code

I have updated my If condition and made the variables public

 public $the_meter_id;
 public $the_meter_msn;

In my 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

                    ]);
}

Then in my 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>

Updated code 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,

            ]);

After running the code I am getting the error Getting unknown property: yii\web\View::the_meter_id in create.php

Any help would be highly appreciated


回答1:


In Controller use ..

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

Silly Mistake .. use

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

Instead of

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

in your create.php



来源:https://stackoverflow.com/questions/46924670/unable-to-render-php-variables-from-controller-to-view-in-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!