I'm not sure if this is Yii's best solution for me. Therefore, I am open to better solutions. Whatever Yii's way of doing this.
I have a left sidebar that should only be displayed to the user who has registered. This is not a problem, I know how to show something by checking "Yii :: $ app-> user-> isGuest".
On specific pages, I don’t want this sidebar, EVEN if they are logged in. For example, on the Contact Us page, you don’t need to have a left sidebar on it. I really don't care if it has a sidebar, but it drops Bootstrap3 columns.
<div class="row"> <div class="col-lg-4 center-col"> <div class="site-contact"> <h1><?= Html::encode($this->title) ?></h1> <p> If you have business inquiries or other questions, please fill out the following form to contact us. Thank you. </p> <div class="row"> <div class="col-lg-12"> [contact form code truncated] </div> </div> </div> </div> </div>
It looks great, centered in the middle of the page, 4 ears wide.
However, in my Yii main.php (frontend / views / layouts / main.php) I have 2 separate layouts, depending on whether the user is registered.
<div class="container-fluid mainpage"> <?= Alert::widget() ?> <div class="row"> <?php if (!Yii::$app->user->isGuest) { ?> <div class="col-md-3 sidebar"> <?= Menu::widget([ 'options' => ['class' => 'nav nav-sidebar'], 'items' => [ ['label' => 'Home', 'url' => ['site/index']], ['label' => 'About', 'url' => ['site/about']], ['label' => 'Contact', 'url' => ['site/contact']], ], ]); ?> </div> <div class="col-md-9"> <?= $content ?> </div> <?php } else { ?> <div class="col-sm-12"> <?= $content ?> </div> <?php } ?> </div> </div>
This code shows a full-width page if it is a guest or sidebar if the user is logged in.
What happens on pages like the Contact Us page is a form, due to the sidebar. The correct content is “col-md-9”, and when you paste a contact form (having “col-lg-4”) into it, it occupies 4 positions within 9 columns, which leads to its smoothing.
Since this page (the contact page) must be publicly accessible (i.e. if the user cannot log in, they will be screwed up and will not be able to contact support), it is possible that both cases can occur. It can be viewed by a registered member, which will lead to a smoothed form or to a guest who will look normal.
My approach was to add the $ showSidebar variable. Then in contact.php there is $ showSidebar = false. The default action for the variable will be set using isGuest, and pages can override it to disable the sidebar for each page.
I really don't need a ton if the checks for isGuest show an alternative div or code, so I think this is the best approach.
How can I expand the view to add my own variable? The file is located in the file vendor / yiisoft / yii2 / web / view. I think..
This is an extended Yii2 template.
PS: I am open to a better solution to my problem. I don't want hacks, but Yii's most correct way to do this.