pkjoshi
(Info)
February 14, 2015, 1:55pm
1
n Yii2 - I have successfully setup authentication module with user setup with database. I can prevent access to specific controller with the following code like:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['login', 'logout', 'signup','index','create','view','delete'],
'rules' => [
[
'allow' => true,
'actions' => ['logout'],
'roles' => ['?'],
],
[
'allow' => true,
'actions' => ['create','delete','index','view','logout'],
'roles' => ['admin'],
],
],
],
];
}
But here the user can access any menu and see the whole application screen.
What I want is a login screen by default before user can access any menu.
Hide the menu item for which user has no access.
I am not sure second one is possible, but I am sure there is some way to create a default screen as login before user can access any menu item.
Thanks.
gugoan
(Gugoan)
February 14, 2015, 4:33pm
2
Hi,
For example: Nav::widget
In propertie Items , use o param visible to to hide the items you want.
Example:
['label' => 'Home ', 'url' => ['/site/index'], 'visible' => !Yii::$app->user->isGuest,],
Rom
(Rokorolov)
February 14, 2015, 4:39pm
3
You can create separate layout for authentication and specifying it in your controller.
Also you can create Asset Bundle for authentication and registering it in layout you don’t have pull all javascript, css, … files from main app to your login page.
pkjoshi
(Info)
February 14, 2015, 4:57pm
4
Rom:
You can create separate layout for authentication and specifying it in your controller.
Also you can create Asset Bundle for authentication and registering it in layout you don’t have pull all javascript, css, … files from main app to your login page.
Thanks Rom,
Can you please elaborate how to do that i.e. create separate layout and specifying that in controller.
pkjoshi
(Info)
February 14, 2015, 5:01pm
5
Hi,
For example: Nav::widget
In propertie Items , use o param visible to to hide the items you want.
Example:
['label' => 'Home ', 'url' => ['/site/index'], 'visible' => !Yii::$app->user->isGuest,],
Thanks Gustavo Andrade, that works fine for hiding a specific menu item/items.
But how can I just provide a login screen as a default screen to the user when he visits the site/application.
Thanks.
Rom
(Rokorolov)
February 14, 2015, 5:22pm
6
In your authentication controller
public $layout = 'authorization';
Create layout in views/layouts/authorization.php
Something like this
<?php
use yii\helpers\Html;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title>
<?= Html::csrfMetaTags() ?>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
pkjoshi
(Info)
February 14, 2015, 8:33pm
7
Rom:
In your authentication controller
public $layout = 'authorization';
Create layout in views/layouts/authorization.php
Something like this
<?php
use yii\helpers\Html;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title>
<?= Html::csrfMetaTags() ?>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
Sorry I am still bit lost.
I am able to set a default view via config using
'defaultRoute' => 'site/login'
The login screen is showing by default, but other menus are visible and accessible.
I do not have any authentication controller. Do I need to create one?
pkjoshi
(Info)
February 15, 2015, 6:37pm
8
Ok Rom, I have created authorization.php with your suggested code in views/layouts.
I have applied the
public $layout = 'authorization';
to site controller, the result is achieved correctly for the login form.
But I am facing the problem like home page layout is also getting changed.
Also how can I make the site/login as the default controller?
If I am setting in config like:
'defaultRoute' => 'site/login',
I am getting the default screen as login, but after login I am getting the error for example
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies.
So I need to resolve two issues.
[list=1]
[*]Home page formatting
[*]Redirecting to home-page with correct layout after login.
[/list]
Thanks.
Rom
(Rokorolov)
February 16, 2015, 10:14am
9
‘But I am facing the problem like home page layout is also getting changed.’
If you have authorization action by default ‘site/login’. Change layout only for login action.
public function actionLogin()
{
$this->layout = 'authorization';
...
}
‘but after login I am getting the error for example’
It’s all happened according to your code, why do you set ‘defaultRoute’ => ‘site/login’? Let it be by default ‘site’ or set it correctly.
pkjoshi
(Info)
February 16, 2015, 3:29pm
10
Rom:
‘But I am facing the problem like home page layout is also getting changed.’
If you have authorization action by default ‘site/login’. Change layout only for login action.
public function actionLogin()
{
$this->layout = 'authorization';
...
}
‘but after login I am getting the error for example’
It’s all happened according to your code, why do you set ‘defaultRoute’ => ‘site/login’? Let it be by default ‘site’ or set it correctly.
Thanks that worked peferectly.