Sitcontroller
<?php
namespace app\controllers;
use app\models\RegisterForm;
use app\models\User;
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'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionRegister()
{
$model = new RegisterForm();
if ($model->load(Yii::$app->request->post())) {
if ($model->validate()) {
// form inputs are valid, do something here
return;
}
}
return $this->renderPartial('register', [
'model' => $model,
]);
}
public function actionIndex()
{
$model = new LoginForm();
$user = new User();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$role = $user->getRole($model->username);
if($role->role == 'A'){
return $this->redirect(array('adminsite/index'));
}elseif ($role->role == 'U'){
return $this->render('about');
}
} else {
return $this->renderPartial('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();
} else {
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();
} else {
return $this->render('contact', [
'model' => $model,
]);
}
}
public function actionAbout()
{
return $this->render('about');
}
}
Adminsitecontroller
<?php
namespace app\controllers;
namespace app\controllers;
use app\models\User;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
class AdminsiteController extends \yii\web\Controller
{
public $layout = 'adminmain';
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actionIndex()
{
if(isset(Yii::$app->session->id)){
return $this->render('index');
}
return $this->redirect(array('site/index'));
}
public function actionTest(){
return $this->render('test');
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}
adminsite/index.php
<?php
/* @var $this yii\web\View */
$this->title = 'Adminss';
$this->params['breadcrumbs'][] = $this->title;
echo $this->render('admin')
?>
adminsite/admin.php
<?php
echo "session".Yii::$app->session->id;
$message = "hello";
echo $message;
?>
In my Layouts/amdinmain.php
I have this code
echo Yii::$app->user->identity->username;
After log-out If I pressed the backbutton,
I have this error
PHP Notice – yii\base\ErrorException
Trying to get property of non-object
in C:\wamp\www\yiibasic\views\layouts\adminmain.php at line 239
</li>
</ul>
<!-- /.dropdown-alerts -->
</li>
<!-- /.dropdown -->
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-user fa-fw"></i> <?php
echo Yii::$app->user->identity->username;?> <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="#"><i class="fa fa-user fa-fw"></i> User Profile</a>
</li>
<li><a href="#"><i class="fa fa-gear fa-fw"></i> Settings</a>
</li>
<li class="divider"></li>
<li> <i class="fa fa-sign-out fa-fw"></i><?= Html::a(Html::encode('Logouts'),$url = ['logout'],$options = ['data-method' => 'post']) ?>
</li>