yiisoft /
yii2-app-basic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace app\controllers; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\filters\AccessControl; |
||
| 7 | use yii\web\Controller; |
||
| 8 | use yii\web\Response; |
||
| 9 | use yii\filters\VerbFilter; |
||
| 10 | use app\models\LoginForm; |
||
| 11 | use app\models\ContactForm; |
||
| 12 | |||
| 13 | class SiteController extends Controller |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * {@inheritdoc} |
||
| 17 | */ |
||
| 18 | public function behaviors() |
||
| 19 | { |
||
| 20 | return [ |
||
| 21 | 'access' => [ |
||
| 22 | 'class' => AccessControl::class, |
||
| 23 | 'only' => ['logout'], |
||
| 24 | 'rules' => [ |
||
| 25 | [ |
||
| 26 | 'actions' => ['logout'], |
||
| 27 | 'allow' => true, |
||
| 28 | 'roles' => ['@'], |
||
| 29 | ], |
||
| 30 | ], |
||
| 31 | ], |
||
| 32 | 'verbs' => [ |
||
| 33 | 'class' => VerbFilter::class, |
||
| 34 | 'actions' => [ |
||
| 35 | 'logout' => ['post'], |
||
| 36 | ], |
||
| 37 | ], |
||
| 38 | ]; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function actions() |
||
| 45 | { |
||
| 46 | return [ |
||
| 47 | 'error' => [ |
||
| 48 | 'class' => 'yii\web\ErrorAction', |
||
| 49 | ], |
||
| 50 | 'captcha' => [ |
||
| 51 | 'class' => 'yii\captcha\CaptchaAction', |
||
| 52 | 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
||
| 53 | ], |
||
| 54 | ]; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Displays homepage. |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function actionIndex() |
||
| 63 | { |
||
| 64 | return $this->render('index'); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Login action. |
||
| 69 | * |
||
| 70 | * @return Response|string |
||
| 71 | */ |
||
| 72 | public function actionLogin() |
||
| 73 | { |
||
| 74 | if (!Yii::$app->user->isGuest) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 75 | return $this->goHome(); |
||
| 76 | } |
||
| 77 | |||
| 78 | $model = new LoginForm(); |
||
| 79 | if ($model->load(Yii::$app->request->post()) && $model->login()) { |
||
| 80 | return $this->goBack(); |
||
| 81 | } |
||
| 82 | |||
| 83 | $model->password = ''; |
||
| 84 | return $this->render('login', [ |
||
| 85 | 'model' => $model, |
||
| 86 | ]); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Logout action. |
||
| 91 | * |
||
| 92 | * @return Response |
||
| 93 | */ |
||
| 94 | public function actionLogout() |
||
| 95 | { |
||
| 96 | Yii::$app->user->logout(); |
||
| 97 | |||
| 98 | return $this->goHome(); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Displays contact page. |
||
| 103 | * |
||
| 104 | * @return Response|string |
||
| 105 | */ |
||
| 106 | public function actionContact() |
||
| 107 | { |
||
| 108 | $model = new ContactForm(); |
||
| 109 | if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { |
||
| 110 | Yii::$app->session->setFlash('contactFormSubmitted'); |
||
| 111 | |||
| 112 | return $this->refresh(); |
||
| 113 | } |
||
| 114 | return $this->render('contact', [ |
||
| 115 | 'model' => $model, |
||
| 116 | ]); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Displays about page. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function actionAbout() |
||
| 125 | { |
||
| 126 | return $this->render('about'); |
||
| 127 | } |
||
| 128 | } |
||
| 129 |