Created
October 30, 2017 12:59
-
-
Save DanielLetto/6ad6a8524c0b5c71f99c1d7263c0cf3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\controllers; | |
use Yii; | |
use yii\filters\AccessControl; | |
use yii\web\Controller; | |
use yii\web\Response; | |
use yii\filters\VerbFilter; | |
use app\models\LoginForm; | |
use app\models\ContactForm; | |
use app\models\User; | |
use app\models\SignupForm; | |
class SiteController extends Controller | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public function behaviors() | |
{ | |
return [ | |
'access' => [ | |
'class' => AccessControl::className(), | |
'only' => ['logout'], | |
'rules' => [ | |
[ | |
'actions' => ['logout'], | |
'allow' => true, | |
'roles' => ['@'], | |
], | |
], | |
], | |
'verbs' => [ | |
'class' => VerbFilter::className(), | |
'actions' => [ | |
'logout' => ['post'], | |
], | |
], | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function actions() | |
{ | |
return [ | |
'error' => [ | |
'class' => 'yii\web\ErrorAction', | |
], | |
'captcha' => [ | |
'class' => 'yii\captcha\CaptchaAction', | |
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, | |
], | |
]; | |
} | |
/** | |
* Displays homepage. | |
* | |
* @return string | |
*/ | |
public function actionIndex() | |
{ | |
return $this->render('index'); | |
} | |
/** | |
* Login action. | |
* | |
* @return Response|string | |
*/ | |
public function actionLogin() | |
{ | |
//Если пользователь авторизирован - редиректим на страницу с игрой | |
if (!Yii::$app->user->isGuest) { | |
// return $this->goHome(); | |
$this->goBack(); | |
} | |
$model = new LoginForm(); | |
if ($model->load(Yii::$app->request->post()) && $model->login()) { | |
//ПОЛЬЗОВАТЕЛЬ УСПЕШНО АВТОРИЗОВАН. ТУТ ПИШЕМ СВОЙ КОД | |
echo 'ПОЛЬЗОВАТЕЛЬ УСПЕШНО АВТОРИЗОВАН. ТУТ ПИШЕМ СВОЙ КОД'; | |
// return $this->goBack(); | |
// $this->goBack(); | |
$this->redirect(['site/about']); | |
// return $this->render('about'); | |
// $this->redirect('site/about',302); | |
} | |
return $this->render('login', [ | |
'model' => $model, | |
]); | |
} | |
/** | |
* Logout action. | |
* | |
* @return Response | |
*/ | |
public function actionLogout() | |
{ | |
Yii::$app->user->logout(); | |
return $this->goHome(); | |
} | |
/** | |
* Displays contact page. | |
* | |
* @return Response|string | |
*/ | |
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, | |
]); | |
} | |
/** | |
* Displays about page. | |
* | |
* @return string | |
*/ | |
public function actionAbout() | |
{ | |
return $this->render('about'); | |
} | |
public function actionSignup() | |
{ | |
$model = new SignupForm(); | |
if ($model->load(Yii::$app->request->post())) { | |
if ($user = $model->signup()) { | |
if (Yii::$app->getUser()->login($user)) { | |
return $this->goHome(); | |
} | |
} | |
} | |
return $this->render('signup', [ | |
'model' => $model, | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment