从视图提交到控制器的绑定值

binding values submitted from a View to a Controller

我正在编写一个表单来获取用户输入,然后将该信息传递给控制器​​并执行基于此的功能,此时我无法使用 POST 方法将数据传递给控制器​​,我得到空变量。

所以 Controller 函数可以正确显示视图表单,我可以在文本框中键入内容,在按下提交按钮后,我收到一条 setFlash 自定义消息,指出参数为空。我使用的模型 class 只有两个参数。

a) 这是模型:

<?php
namespace app\models;
use Yii;
use yii\base\Model;

class SendmailForm extends Model
{
    public $template;
    public $emtransport;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            [['template', 'emtransport'], 'required'],
        ];
    }

}

b) 这是视图:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;

$this->title = 'Send Mail';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
    <h1><?= Html::encode($this->title) ?></h1>

    <?php if (Yii::$app->session->hasFlash('sminfo')): ?>
        <div class="alert alert-success">
            <?= Yii::$app->session->getFlash('sminfo');?>
        </div>

    <?php else: ?>

        <p>
            SendMail Exercise. Please choose needed options bellow:
        </p>

        <div class="row">
            <div class="col-lg-5">

                <?php $form = ActiveForm::begin(['id' => 'sendmail-form']); ?>

                    <?= $form->field($model, 'template')->textInput(['autofocus' => true]) ?>

                    <?= $form->field($model, 'emtransport') ?>

                    <div class="form-group">
                        <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'value'=>'one', 'name'=>'sendbtn']) ?>
                    </div>

                <?php ActiveForm::end(); ?>

            </div>
        </div>

    <?php endif; ?>
</div>

这是控制器的功能:

public function actionSendmail(){
        $model = new SendmailForm();                        
        if ($model->load(Yii::$app->request->post())) {
            $template = Yii::$app->request->post('template');
            $emailTransport = Yii::$app->request->post("emtransport");
            if($emailTransport=="local"){
                for($i=0;$i<=2;$i++){
                    $xclient = 'client' . $i;
                    \app\models\User::findByUsername($xclient)->sendMail($template, 'Welcome To XYZ Services', ['accountInfo' => 'www.mysite.com']);
                }//end of for loop
                Yii::$app->session->setFlash("sminfo", "Emails sent successfully to the Clients");
                return $this->refresh();                
            }//end of second if loop
            else{
                Yii::$app->session->setFlash("sminfo", "Params could not be verified!. Contact Tech Support");
                return $this->refresh();                                
            }

        }//end of post if loop

        return $this->render('sendmail', [
            'model' => $model,
        ]);
    }

想法是从视图中获取值,此时我得到的是空值-

我对 Controller 做了一些更改,现在它可以工作了,就是这些:

//at the beginning of the Controller:
use app\models\SendmailForm;

//two lines changed in the function:
            $model->template = $_POST['SendmailForm']['template'];
            $model->emtransport = $_POST['SendmailForm']['emtransport'];            

这就是我所需要的。最好的问候

以下两部分:

$template = Yii::$app->request->post('template');
$emailTransport = Yii::$app->request->post("emtransport");

更改以下内容:

$template = $model->template;
$emailTransport = $model->emtransport;


编辑后

public function actionSendmail(){
        $model = new SendmailForm();                        
        if ($model->load(Yii::$app->request->post())) {
            $template = $model->template;
            $emailTransport = $model->emtransport;
            if($emailTransport=="local"){
                for($i=0;$i<=2;$i++){
                    $xclient = 'client' . $i;
                    \app\models\User::findByUsername($xclient)->sendMail($template, 'Welcome To XYZ Services', ['accountInfo' => 'www.mysite.com']);
                }//end of for loop
                Yii::$app->session->setFlash("sminfo", "Emails sent successfully to the Clients");
                return $this->refresh();                
            }//end of second if loop
            else{
                Yii::$app->session->setFlash("sminfo", "Params could not be verified!. Contact Tech Support");
                return $this->refresh();                                
            }

        }//end of post if loop

        return $this->render('sendmail', [
            'model' => $model,
        ]);
    }