两种不同的布局在用户 - Yii2来宾用户和已记录

问题描述 投票:2回答:1

此前,我在这里页眉和页脚被宣布main.php。这是确定。现在,我期待为Guest UserRegistered User 2个不同的布局。在这Registered UserAdmin都会有不同的页眉和页脚。

我使用Yii2基本应用。我创建GuestUser.php&RegisteredUser.php views/layouts/文件夹内。

访客用户是指用户未在应用程序登录。而且,注册用户是指用户登录应用程序。

现在,我的目录结构是这样的。

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

现在,我如何使用GuestUser.php&RegisteredUser.php我的应用程序。至于现在,应用程序在默认情况下采取main.php。我该如何控制呢?

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:hello@abc.com">hello@abc.com</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() ?>

我试过了

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();?>
<?}?>

任何帮助/提示/建议是明显的。请帮忙。

php yii2 yii2-basic-app
1个回答
5
投票

实现这一整个应用程序的一种方式可能是设置使用应用程序事件处置和配置布局文件:

//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
    },
    ....
];

如果你想只在单个控制器做到这一点,你可以在控制器中使用layout。只是覆盖在你的控制器中的文件名(不含.PHP)这个成员。

热门问题
推荐问题
最新问题