1 page per yii2
-
We need help. There's a form of search. The man I found on the search needs to send a message. For that, from the search page, I want to send the mail (this is available as a $email) to a man on the mailing model page.
access to the mail is obtained through the results cycle
Total
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> ...
We need to move the mail to the post office.
modules/main/controllers/ContactController.php или в modules/main/models/ContactForm.php
Here's the 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,
]);
}
}
...
SontactForm
<?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; }
}
}
- @return array the validation rules.
and drink it
<?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>
-
-
I did it. Realized the idea that the Ninazu gave me.
<?php echo Html::a( 'Передать сюда email', Url::to(['default/contact', 'email' => $email]) ); ?>
And then in the right vein, you did it.
<?php if(isset($_GET['email'])) { $model -> email = $_GET['email']; } ?>
Also found a way of working with sessions
Indicate on the veil:
\Yii::$app->session->set('email', 'email@example.com');
And where the data should be retrieved,
\Yii::$app->session->get('email');
You can also use Flash:
Yii::$app->session->setFlash('email', 'email@example.com');
echo Yii::$app->session->getFlash('email');