Default screen as login Screen

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.



For example: Nav::widget

In propertie Items, use o param visible to to hide the items you want.


['label' => 'Home ', 'url' => ['/site/index'], 'visible' => !Yii::$app->user->isGuest,], 

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.

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.


In your authentication controller

    public $layout = 'authorization';

Create layout in views/layouts/authorization.php

Something like this


use yii\helpers\Html;


<?php $this->beginPage() ?>

<!DOCTYPE html>

<html lang="<?= Yii::$app->language ?>">


    <meta charset="<?= Yii::$app->charset ?>"/>

    <title><?= Html::encode($this->title) ?></title>

    <?= Html::csrfMetaTags() ?>

    <?php $this->head() ?>



<?php $this->beginBody() ?>

    <?= $content ?>

<?php $this->endBody() ?>



<?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?

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.


[*]Home page formatting

[*]Redirecting to home-page with correct layout after login.



‘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.

Glad to hear it, :)