2016-11-07 81 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