2016-11-07 80 views
1

在actionLayer中单击prict按钮时,我想将数据传递给page_printinvoice。这部分工作正常。现在,我想在传递到视图页面之前访问if语句。如果控制台是GM,我想传递给_printinvoicegm,并且控制台是SM。我想传递给_printinvoicesm。我已经在控制器操作中尝试了以下代码。如果在yii2中条件重定向

public function actionPrintinvoice($id) { 

     $model = Bills::find()->where(['bills_ebillid' => $id])->one(); 
     $searchModel = new BillsSearch(); 
     $searchModel->console = $consoleid; 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $consoleid); 
     //$data   = Sellsg::findOne($id); 
     $searchModel1 = new ProductsalesSearch(); 
     $searchModel1->productsales_ebillid = $id; 
     $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams); 

     // $modelOffer = Offers::find()->orderBy('of_id')->limit(1)->one(); 
     // $searchModel2 = new OffersSearch();  
     // $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams); 

     if($consoleid == 'GM1' || $consoleid == 'GM2'){ 
      $content = $this->renderPartial('_printinvoicegm', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>GM</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 

     }elseif($consoleid == 'SM1' || $consoleid == 'SM2'){ 
      $content = $this->renderPartial('_printinvoicegm', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>SM</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 

     }elseif($consoleid == 'CN'){ 
      $content = $this->renderPartial('_printinvoicegm', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>CN</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 

     } 


     return $pdf->render(); 
     //return $this->render('_printSalarystatement', ['s_period' => $s_period]); 

    } 

在这段代码中,我得到的错误 -

Undefined variable: consoleid 

请帮助。

更新

public function actionPrintinvoice($id) { 

     $consoleid = 'My_console_id'; 
     $model = Bills::find()->where(['bills_ebillid' => $id])->one(); 
     $searchModel = new BillsSearch(); 
     $searchModel->console = $consoleid; 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
     //$data   = Sellsg::findOne($id); 
     $searchModel1 = new ProductsalesSearch(); 
     $searchModel1->productsales_ebillid = $id; 
     $dataProvider1 = $searchModel1->search(Yii::$app->request->queryParams); 

     // $modelOffer = Offers::find()->orderBy('of_id')->limit(1)->one(); 
     // $searchModel2 = new OffersSearch();  
     // $dataProvider2 = $searchModel2->search(Yii::$app->request->queryParams); 
     if($consoleid == 'GM1' || $consoleid == 'GM2'){ 
      $content = $this->renderPartial('_printinvoicegm', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>GM</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 
     return $pdf->render(); 
     }elseif($consoleid == 'SM1' || $consoleid == 'SM2'){ 
      $content = $this->renderPartial('_printinvoicesm', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>SM</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 
     return $pdf->render(); 
     } 
     elseif($consoleid == 'CN'){ 
      $content = $this->renderPartial('_printinvoicecn', [ 
      'model' => $model, 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 

      'searchModel1' => $searchModel1, 
      'dataProvider1' => $dataProvider1, 

      // 'modelOffer' => $modelOffer, 
      //'searchModel2' => $searchModel2,   
      //'dataProvider2' => $dataProvider2, 
      ]); 
     $footer = "<table name='footer' width=\"1000\"> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\"><u>CN</u></td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">9:00am to 9:00pm</td> 
      </tr> 
      <tr> 
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"left\">Friday Morning Closed</td>    
      <td style='font-size: 18px; padding-bottom: 20px;' align=\"right\">Prop:  M.Sinha</td> 
      </tr> 
     </table>"; 
     $pdf = new Pdf([ 
      'mode'=> Pdf::MODE_UTF8, 
      'format'=> Pdf::FORMAT_A4, 
      'destination'=> Pdf::DEST_BROWSER, 
      //'destination' => Pdf::DEST_DOWNLOAD, 
      'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
      // any css to be embedded if required 
      'cssInline' => '.kv-heading-1{font-size:18px}', 
      // set mPDF properties on the fly 
      'options' => ['title' => 'Print Invoice'], 
      //'options' => ['defaultfooterline' => 0,], 
      // call mPDF methods on the fly 
      'methods' => [ 
       //'SetHeader'=>['Amit Optical'], 
       'SetFooter'=>[$footer], 
      ], 
      'content' => $content, 

     ]); 
     return $pdf->render(); 
     } 
     //return $this->render('_printSalarystatement', ['s_period' => $s_period]); 

    } 

该指数看起来像下面 - enter image description here

我点击打印按钮重定向到respectiveprint页面。 动作栏的代码是像下面 - 现在

[ 
      'class' => 'kartik\grid\ActionColumn', 
      'template' => '{update} {print}', 
      'buttons' => [ 
       'print' => function ($url, $model) { 
        return Html::a(
         '<span class="glyphicon glyphicon-print"></span>', 
         ['/invoice/bills/printinvoice', 'id' => $model->bills_ebillid,'cons' => $model->console ], 
         [ 
          'title' => 'Print', 
          'data-pjax' => '0', 
         ] 
        ); 
       }, 
       ], 
      ], 

,控制台会传送到URL,但我不能用它来重定向相​​应的不同页面。 enter image description here

+0

因为你从未宣布过,也没有定义'$ consoleid'此信息。什么是'$ searchModel-> console'? –

回答

1

您正在使用分配一个瓦尔未previosly用适当的内容定义

public function actionPrintinvoice($id) { 

    $model = Bills::find()->where(['bills_ebillid' => $id])->one(); 
    $searchModel = new BillsSearch(); 

//这个$ consoleid没有定义,但你尝试访问

$searchModel->console = $consoleid; 

你应该在使用前定义一个合适的数值,例如:

$consoleid = 'My_console_id' 

。 公共职能actionPrintinvoice($ ID){

$consoleid = 'My_console_id' 
$model = Bills::find()->where(['bills_ebillid' => $id])->one(); 
$searchModel = new BillsSearch(); 

更新评论太长

我已经设置好的$ consoleid =“My_c​​onsole_id”仅通知您,对于错误的原因是dovita的事实变量在正确分配之前使用。 :事实上,您的代码将该值分配给用于搜索要选择的数据的参数..我无法知道什么是正确的值分配给该参数使有效的搜索(你应该知道)..这可能是你没有得到nessu结果然后得到一个空白页面的原因。因此,您应该尝试了解您的应用程序必须分配给$ consoleid的有效值,因为您可以找到正确的数据,然后允许打印。这也意味着我的答案已解决了您所报告的问题..

看你的代码为printinvoce ..你通过身份证和控制台

['/invoice/bills/printinvoice', 'id' => $model->bills_ebillid,'cons' => $model->console ], 

..你retrive使用

$model = Bills::find()->where(['bills_ebillid' => $id])->one(); 

,那么你应该更改功能

public function actionPrintinvoice($id, $cons) { 
...... 

的签名,并分配

$searchModel->console = $cons; 
+0

我应该如何定义它? – Tanmay

+0

我不知道这个var ..的意思,你只是声明和分配一个值..我已经更新了答案..(你应该评估分配一个合适的内容) – scaisEdge

+0

我已经添加了$ consoleid ='My_console_id '如您所示,并通过$ searchModel-> console = $ consoleid;我试图将控制台从特定行传递给变量$ consoleid。请让我知道如果我做错了或如何做到这一点。我在更新的问题中添加了我目前的情况。现在没有错误,但是当我点击action列中的打印按钮时,我得到一个空白页。我可以在url中看到id已经通过,但没有显示consoleid的标志。请告诉我如何做到这一点。 – Tanmay