Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calcio/6a5d45ceb33fde835ddbf64bf2c789b9 to your computer and use it in GitHub Desktop.
Save calcio/6a5d45ceb33fde835ddbf64bf2c789b9 to your computer and use it in GitHub Desktop.
Vitrine - Module Admin - DefaultController.php
<?php
namespace app\modules\admin\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\modules\admin\models\LoginForm;
/**
* Default controller for the `admin` module
*/
class DefaultController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index', 'logout', 'dashboard'],
'rules' => [
[
'actions' => ['index'],
'allow' => true,
'roles' => ['?'],
],
[
'actions' => ['logout', 'dashboard'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
if (!Yii::$app->user->isGuest) {
return $this->render('dashboard');
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->render('dashboard');
}
return $this->render('index', ['model' => $model]);
}
public function actionDashboard()
{
return $this->render('dashboard');
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->redirect(['default/index']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment