Advertisement
Guest User

Untitled

a guest
Mar 14th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\models\Blogger;
  6. use app\models\RegisterForm;
  7. use Yii;
  8. use yii\filters\AccessControl;
  9. use yii\web\Controller;
  10. use yii\filters\VerbFilter;
  11. use app\models\LoginForm;
  12. use app\models\ContactForm;
  13.  
  14. class SiteController extends Controller
  15. {
  16.     /**
  17.      * @inheritdoc
  18.      */
  19.     public function behaviors()
  20.     {
  21.         return [
  22.             'access' => [
  23.                 'class' => AccessControl::className(),
  24.                 'only' => ['logout'],
  25.                 'rules' => [
  26.                     [
  27.                         'actions' => ['logout'],
  28.                         'allow' => true,
  29.                         'roles' => ['@'],
  30.                     ],
  31.                 ],
  32.             ],
  33.             'verbs' => [
  34.                 'class' => VerbFilter::className(),
  35.                 'actions' => [
  36.                     'logout' => ['post'],
  37.                 ],
  38.             ],
  39.         ];
  40.     }
  41.  
  42.     /**
  43.      * @inheritdoc
  44.      */
  45.     public function actions()
  46.     {
  47.         return [
  48.             'error' => [
  49.                 'class' => 'yii\web\ErrorAction',
  50.             ],
  51.             'captcha' => [
  52.                 'class' => 'yii\captcha\CaptchaAction',
  53.                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  54.             ],
  55.         ];
  56.     }
  57.  
  58.     /**
  59.      * Displays homepage.
  60.      *
  61.      * @return string
  62.      */
  63.     public function actionIndex()
  64.     {
  65.         return $this->render('index');
  66.     }
  67.  
  68.     /**
  69.      * Login action.
  70.      *
  71.      * @return string
  72.      */
  73.     public function actionLogin()
  74.     {
  75.         if (!Yii::$app->user->isGuest) {
  76.             return $this->goHome();
  77.         }
  78.  
  79.         $model = new LoginForm();
  80.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  81.             return $this->goBack();
  82.         }
  83.         return $this->render('login', [
  84.             'model' => $model,
  85.         ]);
  86.     }
  87.  
  88.     public function actionRegister()
  89.     {
  90.         $model = new RegisterForm();
  91.         $user = new Blogger();
  92.  
  93.         if(isset($_POST['RegisterForm']))
  94.         {
  95.             $model->attributes = $_POST['RegisterForm'];
  96.  
  97.             if($model->validate())
  98.             {
  99.                 $user->username = $model->username;
  100.                 $user->email = $model->email;
  101.                 $user->password = $model->password;
  102.                 $user->save();
  103.  
  104.                 $this->redirect('index');
  105.             }
  106.         }
  107.  
  108.         return $this->render('register',[
  109.             'model' => $model,
  110.         ]);
  111.     }
  112.  
  113.     /**
  114.      * Logout action.
  115.      *
  116.      * @return string
  117.      */
  118.     public function actionLogout()
  119.     {
  120.         Yii::$app->user->logout();
  121.  
  122.         return $this->goHome();
  123.     }
  124.  
  125.     /**
  126.      * Displays contact page.
  127.      *
  128.      * @return string
  129.      */
  130.     public function actionContact()
  131.     {
  132.         $model = new ContactForm();
  133.         if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  134.             Yii::$app->session->setFlash('contactFormSubmitted');
  135.  
  136.             return $this->refresh();
  137.         }
  138.         return $this->render('contact', [
  139.             'model' => $model,
  140.         ]);
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement