在yii2中重定向if条件

时间:2016-11-07 21:21:37

标签: yii2

单击actioncolumn中的prict按钮时,我想将数据传递给page _printinvoice。这部分工作正常。现在,我想在传递给视图页面之前使用if语句。如果控制台是GM,我想传递给_printinvoicegm,如果控制台是SM。我想传递给_printinvoicesm。我在控制器操作中尝试了以下代码。

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

我点击打印按钮重定向到相应的打印页面。 action列的代码如下所示 -

[
            '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',
                        ]
                    );
                },
                ],
            ],

现在,控制台正在传递给网址,但我无法使用它来相应地重定向到不同的网页。 enter image description here

1 个答案:

答案 0 :(得分:1)

您正在使用vars进行分配,而这些分配并未使用适当的内容进行预先定义

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_console_id'只是为了通知你错误的原因是dovita在正确分配之前使用了变量的事实。 :事实仍然是您的代码将该值分配给用于搜索要选择的数据的参数。我不知道要在有效搜索上分配给此参数的正确值(您应该知道)..这可能是你没有得到nessu结果,然后得到一个空白页面的原因。因此,您应该尝试知道您的应用程序必须分配给$ consoleid的有效值,因为您可以找到正确的数据然后允许打印..这也意味着我的答案已经解决了您报告的问题..

查看printinvoce的代码..你传递为id和console

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

..然后使用

检索此信息
  $model = Bills::find()->where(['bills_ebillid' => $id])->one(); 

然后你应该改变功能的签名

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

并指定

$searchModel->console = $cons;