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

Posted

技术标签:

【中文标题】无法从控制器渲染 php 变量以在 yii2 中查看【英文标题】:Unable to render php variables from controller to view in yii2 【发布时间】:2018-04-06 02:03:45 【问题描述】:

我是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解决方案编写的。当我运行此代码时,我在create.php 中收到错误Undefined variable: the_meter_id

我做了什么让事情顺利进行?

我已经尝试了每一种可能的方法来渲染/传递我的变量到视图,比如将变量 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,

            ]);

运行代码后,我在create.php 中收到错误Getting unknown property: yii\web\View::the_meter_id

任何帮助将不胜感激

【问题讨论】:

告诉我你的create.php @DoubleH 请查看更新后的代码,我已将完整的create.php 这是您完整的 create.php .. 似乎您没有将 'the_meter_id'=&gt;$the_meter_id, 'the_meter_msn'=&gt; $the_meter_msn 传递给您的 _form.php @DoubleH,是的,抱歉我的错误我现在添加了它们 在下面查看我的回答 【参考方案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

【讨论】:

再次出现错误Undefined variable: the_meter_id 这次是什么完整的错误 $this-&gt;render('_form', [ 'model' =&gt; $model, 'the_meter_id'=&gt;$the_meter_id, 'the_meter_msn'=&gt;$the_meter_msncreate.php action 中显示return $this-&gt;render('create', [ 'model' =&gt; $model, ]); 在控制器中使用` $this->render('create', [ 'model' => $model,'the_meter_id'=>$the_meter_id, 'the_meter_msn'=>$the_meter_msn ]);`渲染视图时,,,

以上是关于无法从控制器渲染 php 变量以在 yii2 中查看的主要内容,如果未能解决你的问题,请参考以下文章

Yii2 使用查询参数渲染 url

如何将参数从控制器传递到 YII2 中的布局

Yii2基本模式不调用main.php

如何从控制器渲染两种类型的数据以在 node.js 中查看?

Yii2 - 在视图的 index.php 中渲染 _search.php 文件

Yii2.0框架 控制器 Controllers