如何从yii2中的另一个模型或控制器中的一个视图表单中获取值?

时间:2016-08-10 11:44:54

标签: yii2

我需要你的帮助。我有查看表单search.php(modules / user / default / search)。我需要从这个搜索表单中找到用户的电子邮件($ email)转移到ContactForm(modules / main / models / ContactForm)或ContactController(modules / main / controllers / ContactController)。我怎么能达到这个目标?

这是我的鳕鱼来自搜索表格

<?php
$this->title = Yii::t('app', 'TITLE_SEARCH');
?>
<?php use yii\helpers\Html; ?><br>
<div class="text-left"><a href="<?='/taxi/web/user/default/index'?>" class="btn" style="background-color: honeydew" >Назад к Поиску</a></div>
<br><br>


<?php if (!$result) { ?>
    <p>Ничего не найдено</p>
<?php } else { ?>
    <?php foreach ($result as $one){
        $from = $one -> from;
        $to = $one -> to;
        $age = $one -> age;
        $username = $one -> username;
        $usersurname = $one -> usersurname;
        $data = $one -> data;
        $time = $one -> time;
        $price = $one -> price;
        $place = $one -> place;
        $email = $one -> email;
        $id = $one -> id;
        ?>



            <div class="container" style=" border-radius: 5px">
                    <div class="col-xs-1"></div>
                    <div class="col-xs-10" style="background-color:mintcream; border-radius: 5px">
                        <div class="col-xs-3" style="">
                            <div class="row">
                                <div class="col-xs-12 text-center">
                                    <h4><?=$username?>&nbsp;<?=$usersurname?></h4>
                                    <p><?= Yii::t('app', 'TITLE_AGE')?>:<?= $age ?></p>
                                    <div class="col-xs-12 text-right">
                                        <a class="btn siteColor2" href='<?=Yii::$app->urlManager->createUrl(["user/profile/user"])?>'> <?=Yii::t('app', 'BUTTON_DRIVER_PAGE'); ?></a>
                                    </div>
                                </div>
                            </div><br>
                        </div>
    ...

我需要将此电子邮件转发到modules/main/controllers/ContactController.php或发送给。{ modules/main/models/ContactForm.php

这是来自ContactController的行动

namespace app\modules\main\models;

use Yii;
use yii\base\Model;    
class ContactForm extends Model
{
...
    public function actionIndex()
        {
            $model = new ContactForm();
            if ($user = Yii::$app->user->identity) {
                /** @var \app\modules\user\models\User $user */
            }
            if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('contactFormSubmitted');
                return $this->refresh();
            } else {
                return $this->render('index', [
                    'model' => $model,
                ]);
            }
        }
    ...

modelСontactForm

<?php

namespace app\modules\main\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model
{
    public $name;
    public $email;
    public $subject;
    public $body;
    public $verifyCode;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'email', 'subject', 'body'], 'required'],
            // email has to be a valid email address
            ['email', 'email'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha', 'captchaAction' => '/main/contact/captcha'],
        ];
    }

    /**
     * @return array customized attribute labels
     */


    public function attributeLabels()
    {
        return [
            'verifyCode' => Yii::t('app', 'USER_VERIFYCODE'),
            'name' => Yii::t('app', 'USER_USERNAME'),
            'email' => Yii::t('app', 'USER_EMAIL'),
            'subject' => Yii::t('app', 'USER_SUBJECT'),
            'body' => Yii::t('app', 'USER_BODY'),
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     * @param string $email the target email address
     * @return boolean whether the model passes validation
     */

    public function contact($email)
    {
        if ($this->validate()) {
            Yii::$app->mailer->compose()
                ->setTo($email)
                ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
                ->setReplyTo([$this->email => $this->name])
                ->setSubject($this->subject)
                ->setTextBody($this->body)
                ->send();

            return true;
        } else {
            return false;
        }
    }
}

和他的观点

<?php

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \app\modules\main\models\ContactForm */

use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;

$this->title = Yii::t('app', 'TITLE_CONTACT');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="main-contact-index">
    <h1><?= Html::encode($this->title) ?></h1>

    <?php if (Yii::$app->session->hasFlash('contactFormSubmitted')): ?>

        <div class="alert alert-success">
            <?= Yii::t('app', 'CONTACT_THANKS'); ?>
        </div>

    <?php else: ?>

        <div class="row">
            <div class="text-left"><a href="<?='/taxi/web/user/'?>" class="btn siteColor2" style="background-color: honeydew" >Назад</a></div><br>
            <div class="col-md-3"></div>
            <div class="col-md-6 text-left">
                <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
                <?= $form->field($model, 'name') ?>
                <?= $form->field($model, 'email') ?>
                <?= $form->field($model, 'subject') ?>
                <?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
                <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
                    'captchaAction' => '/main/contact/captcha',
                    'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
                ]) ?>
                <div class="form-group">
                    <?= Html::submitButton(Yii::t('app', 'BUTTON_SEND'), ['class' => 'btn siteColor', 'name' => 'contact-button']) ?>

                </div>
                <?php ActiveForm::end(); ?>
            </div>
            <div class="col-md-3"></div>
        </div><br><br><br>

    <?php endif; ?>
</div>

1 个答案:

答案 0 :(得分:1)

您好,您可以尝试会议:

在您的视图中设置

\Yii::$app->session->set('email', 'emil@example.com');

您想要检索的位置:

\Yii::$app->session->get('email'); 

或使用闪光灯:

Yii::$app->session->setFlash('email', 'emil@example.com');

 echo Yii::$app->session->getFlash('email');

More About Yii2 Session and Flashes