Search code examples
phpyii2yii2-basic-app

Two different layouts for Guest User & Logged in User- Yii2


Previously, I was having main.php where header & footer were declared. Which is Ok. Now, I am looking for 2 different layouts for Guest User & Registered User. In which Registered User and Admin will have different header & footer.

I am using Yii2 Basic App. I created GuestUser.php & RegisteredUser.php inside views/layouts/ folder.

Guest User means User has not logged in the application. And, Registered User means User Has logged in the Application.

Now, My directory structure is like this.

views
    ->layouts
        ->main.php
        ->GuestUser.php
        ->RegisteredUser.php
    ->site  

Now, How can I use GuestUser.php & RegisteredUser.php for my application. As for now, Application is taking main.php by default. How can I control this ?

GuestUser.php

<?php
use yii\helpers\Html;
use app\assets\LocalAsset;
LocalAsset::register($this);
?>
<?php $this->beginPage() ?>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title>ABC</title>
    <?php $this->head() ?>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Oswald:400,700' rel='stylesheet' type='text/css'>
</head>
<body>
  <?php $current_controller_id = Yii::$app->controller->module->id;?>
  <?php $this->beginBody() ?>
  <?php $baseUrl = Yii::$app->params['baseUrl'];?>
  <div class="container">
    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
      <div class="full-width grey bottom-header">
        <div class="top_section-bg">
          <div class="top_section container">
            <ul> 
              <li>Have any Questions?&nbsp;</li>                         
              <li>
                <i class="fa fa-phone fa-lg"></i>
                <span class="header-small-text">+1 &nbsp;1234567890&nbsp;&nbsp;&nbsp;&nbsp;</span>
              </li> 
              <li>
                <i class="fa fa-envelope-o fa-lg"></i>
                <span class="header-small-text">&nbsp;<a href="mailto:[email protected]">[email protected]</a></span>
              </li>                     
            </ul> 
          </div>
        </div>
        <div class="menu-bg">
          <div class="container">
            <div class="navbar-header big_log" >
              <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
              </button>
              <a class="navbar-brand" href="index.php"><img src="<?php echo $baseUrl;?>images/logo.png" class="logo"  id="logo"></a>
            </div>

            <div class="collapse navbar-collapse menu_font" id="bs-example-navbar-collapse-1">
              <ul class="nav navbar-nav navbar-right">
                <li class=""><a href="about.php">About</a></li> 
              </ul>
            </div>
          </div>
        </div>
      </div>
    </nav>
  </div>

  <?=$content;?>

  <section class="section11">
    <div class="col-sm-12 copy">
      &copy;Copyright ABC, 2016. All Rights Reserved. <span class="privacy"> <a href="">Privacy</a></span> | <span class="privacy">
      <a href=""> Terms & Conditions</a></span>
      </ul>
    </div>
  </section>  
  <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

RegisteredUser.php

<?php
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use app\assets\AppAsset;
use app\models\MyActiveRecord as AR;

AppAsset::register($this);
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>
    <title>ABC</title>
    <?php $this->head() ?>
</head>
<body>
  <?php $current_controller_id = Yii::$app->controller->module->id;?>
  <?php $this->beginBody() ?>
  <div class="wrap">
    <?php
    $baseUrl = Yii::$app->params['baseUrl'];
    NavBar::begin([
        'brandLabel' => '<img src="'.$baseUrl.'logo/ABC.png" style="margin-top:-15px;">',
        'brandUrl' => Yii::$app->homeUrl,
        'options' => [
            'class' => 'navbar-inverse navbar-fixed-top',
        ],
    ]);
    NavBar::end();
    ?>

  <footer class="footer">
      <div class="container"> 
          <p class="text-center">&copy; ABC<?= date('Y') ?></p>
      </div>
  </footer>
  <?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

I Tried

main.php

<?php if(Yii::$app->user->isGuest){?>
    <?php $this->beginContent('@app/views/layouts/GuestUser.php'); ?>
            <?=$content;?>
    <?php $this->endContent();?>
<?php } else {?>
    <?php $this->beginContent('@app/views/layouts/RegisteredUser.php'); ?>
            <?=$content;?>
    <?php $this->endContent();?>
<?}?>

Any help/hint/suggestions is appreciable. Please help.


Solution

  • One way to achieve this for the whole application might be to set use an application event handler and configure the layout file:

    //in web.php
    
    $config = [
        ....
        'on beforeRequest' => function ($event) {
            Yii::$app->layout = Yii::$app->user->isGuest ? 
                '@app/views/layouts/GuestUser.php' :      // or just use 'GuestUser' and 
                '@app/views/layouts/RegisteredUser.php';  // 'RegisteredUser' since the path 
                                                          // and the extension are used by
                                                          // default
        },
        ....
    ];
    

    If you want to do it only within a single controller you could use layout in the controller. Just override this member with the file name (without .php) in your controller.