Help is needed. There is a view of the search form. Found a person with a search to send a message. To do this, from the search page I want to send mail (there it is available as $ email) of a person to the page of the message sending model.

access to the mail I get through the cycle of the results found

in modules/user/view/default/search.php

  <?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> ... 

you need to forward mail to modules/main/controllers/ContactController.php или в modules/main/models/ContactForm.php

Here is an action from 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 */ $model->name = $user->username; $model->email = $user->email; } 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, ]); } } ... 

СontactForm model

 <?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; } } } 

and his twist

 <?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> 
  • Yii2 like MVC framework. What are you doing .... Read about the concept of MVC - Ninazu
  • @Ninazu I use the MVC rule. all view have their own models and actions in their controllers. I just don’t know how to pass a parameter from search forms to contact. I would like to receive this parameter in ContactController or in the ContactForm model. Added ContactForm, ContactController and contact / index to a question for clarity - Ildar Nasurlaev
  • So it is more understandable) To form a link with the transition to the page of sending a message with an ID or email user is not an option? Well, or if you are sending via AJAX, then take the value from the generated attribute - Ninazu
  • @Ninazu Thanks for the idea with the link. I'll try as you said. What about giving me a rating? - Ildar Nasurlaev

1 answer 1

I managed. Implemented the idea with a link suggested by Ninazu.

  <?php echo Html::a( 'Передать сюда email', Url::to(['default/contact', 'email' => $email]) ); ?> 

And then in the right twist did

  <?php if(isset($_GET['email'])) { $model -> email = $_GET['email']; } ?> 

I also found a way to work with sessions.

set to view:

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

And where you need to extract the data to register:

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

You can also use Flash:

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