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

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

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

      }
      }

    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>

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

    </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');


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2