<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'actions' => ['captcha','index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post','get'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/* public function actionIndex()
{
return $this->render('index');
}*/
public function actionIndex()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS)) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
} else {
return $this->render('index', [
'model' => $model,
]);
}
}
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
/*
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
*/
public function actionAbout()
{
return $this->render('about');
}
}
<?php
/**
* Created by PhpStorm.
* User: Andrey
* Date: 14.05.2016
* Time: 10:37
*/
namespace app\controllers;
use Yii;
use yii\App\Controller;
use app\models\ContactForm;
use yii\web\Request;
class IndexController extends AppController
{
// экшен корзины
public function actionContact()
{
//$this->layout = 'contact';
$this->layout = false;
/* Создаем экземпляр класса */
$model = new ContactForm();
/* получаем данные из формы и запускаем функцию отправки contact, если все хорошо, выводим сообщение об удачной отправке сообщения на почту */
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('contact', [
'model' => $model,
]);
/* return $this->render('contact', compact('contact'));*/
}
}
}
/* public function actionView(){
// выводим title E-SHOPPER и прицепляем название категории
/* $this->setMeta('E-SHOPPER | ' . $category->name, $category->keywords, $category->description);
// рендерим category/view и передаем в него найденные продукты
return $this->render('index', compact('contact'));
}
}
*/
<?php
namespace app\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'],
];*/
/* Поля обязательные для заполнения */
[ ['name', 'email', 'subject', 'body'], 'required'],
/* Поле электронной почты */
['email', 'email'],
/* Капча */
['verifyCode', 'captcha', 'captchaAction'=>'index/captcha'],
/* ['verifyCode', 'captcha','captchaAction'=>'/contactus/default/captcha'],*/
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Подтвердите код',
'name' => 'Имя',
'email' => 'Электронный адрес',
'subject' => 'Тема',
'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([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}*/
/* функция отправки письма на почту */
public function contact($emailto)
{
/* Проверяем форму на валидацию */
if ($this->validate()) {
Yii::$app->mailer->compose()
->setFrom([$this->email => $this->name]) /* от кого */
->setTo($emailto) /* куда */
->setSubject($this->subject) /* имя отправителя */
->setTextBody($this->body) /* текст сообщения */
->send(); /* функция отправки письма */
return true;
} else {
return false;
}
}
}
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
]
];
}