In my main.php, I have a menu:
'items' => [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'Biz Staff', 'url' => ['bizstaff/index'], 'visible' => User::isBizAdmin(), 'items' => [
['label' => 'Staff List', 'url' => 'index.php?r=user/index'],
['label' => 'Add Staff', 'url' => 'index.php?r=user/create'],
]],
['label' => 'Transaction', 'url' => ['transactions/index'], 'visible' => User::isBizAdmin() || User::isBizStaff(), 'items' => [
['label' => 'Transactions', 'url' => 'index.php?r=transactions/index'],
['label' => 'Add Transactions', 'url' => 'index.php?r=transactions/create'],
]],
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
Here, I am logged in as the superadmin (note that only Home and Logout menu is visible to superadmin user). Inside the homepage (Home menu) is a List of Biz Admins which is placed inside a GridView widget. It has an Action column where view, update and delete icons are placed. When I click the view icon of a specific Biz Admin, it will then render a detailed view page of that Biz Admin where its Store Name and List of Staff are seen. In this page, there is a View Store button which will redirect to the bizadmin view/page.
Whenever superadmin lands on bizadmin view/page, the menu bar should now change to:
Home, Biz Staff, Transaction, Logout
How do I do this? Is it set in the visible attribute?
Any of your answers would be highly appreciated. I am currently stuck in this problem.
Yes. You should use visible property. 'visible' => true or 'visible' => false.
Or you may assemble an array. Like that:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
See in Advanced template - https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/views/layouts/main.php
You can make use of Yii::$app->controller->action->id or Yii::$app->controller->id to render menus based on your path.
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (Yii::$app->controller->action->id == 'put your id here') {
$menuItems[] = ['render your menu here'];
} else {
'do something here'
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
Related
I have a yii2 application, and this is a part of my layouts/main.php view:
<?php
/* #var $this \yii\web\View */
/* #var $content string */
...
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<?php
NavBar::begin([
'brandLabel' => Yii::t('app', Yii::$app->name),
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-default navbar-fixed-top',
],
]);
// everyone can see Home page
$menuItems[] = ['label' => Yii::t('app', 'Home'), 'url' => ['/index.php/']];
// we do not need to display About and Contact pages to employee+ roles
if (!Yii::$app->user->can('employee')) {
//$menuItems[] = ['label' => Yii::t('app', 'About'), 'url' => ['/site/about']];
$menuItems[] = ['label' => Yii::t('app', 'Contact'), 'url' => ['/site/contact']];
}
// display Users to admin+ roles
if (Yii::$app->user->can('admin')){
$menuItems[] = ['label' => Yii::t('app', 'Users'), 'url' => ['/user/index']];
}
// display Logout to logged in users
if (!Yii::$app->user->isGuest) {
//add items, rooms etc. to menu for logged in users
$menuItems[] = ['label' => Yii::t('app', 'Personnel'), 'url' => ['/person/index']];
$menuItems[] = ['label' => Yii::t('app', 'Items'), 'url' => ['/item/index']];
$menuItems[] = ['label' => Yii::t('app', 'Locations'), 'url' => ['/location/index/']];
$menuItems[] = ['label' => Yii::t('app', 'Buildings'), 'url' => ['/building/index/']];
$menuItems[] = ['label' => Yii::t('app', 'Rooms'), 'url' => ['/room/index/']];
$menuItems[] = [
'label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
// display Signup and Login pages to guests of the site
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => Yii::t('app', 'Signup'), 'url' => ['/site/signup']];
$menuItems[] = ['label' => Yii::t('app', 'Login'), 'url' => ['/site/login']];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
As you can see, I have location, room and building in my menu. However, I'd like to only have location in the main menu, and room and building in location's sub-menu. Is there a simple way to do this?
I don't know what exactly your sub-menu is, if you only want to generate a dropdown list, you may
// display Logout to logged in users
if (!Yii::$app->user->isGuest) {
//add items, rooms etc. to menu for logged in users
$menuItems[] = [
'label' => Yii::t('app', 'Locations'),
'url' => ['/location/index/'],
'items' => [
['label' => Yii::t('app', 'Personnel'), 'url' => ['/person/index']],
['label' => Yii::t('app', 'Items'), 'url' => ['/item/index']],
['label' => Yii::t('app', 'Buildings'), 'url' => ['/building/index/']],
['label' => Yii::t('app', 'Rooms'), 'url' => ['/room/index/']],
[
'label' => Yii::t('app', 'Logout'). ' (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
];
}
I'm trying to create a page with a menu which has a different visibility for different users. I have 3 users: Users, Moderators and admins. Could somebody help me with this issue. Here is my code:
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user')],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator')],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin')],
],
]);
visible option is boolean. Defaults to true.
show this Menu widget
also show the getIdentity()
try below code
echo Menu::widget([
'items' => [
['label' => 'Acties', 'visible' => Yii::$app->user->getIdentity('user') ? true : false],
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file', 'visible' => Yii::$app->user->getIdentity('moderator') ? true : false],
['label' => 'Beheer afbeeldingen', 'url' => ['admin'], 'icon' => 'list-alt', 'visible' => Yii::$app->user->getIdentity('admin') ? true : false],
],
]);
You try to right if condition example:
if(Yii::$app->user->getIdentity('moderator')){
echo Menu::widget([
'items' => [
['label' => 'Maak afbeeldingen aan', 'url' => ['create'], 'icon' => 'file'],
],
]);
}
Shorthand if in a PHP array won't work, even if I surround the true in braces.
The working code:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
Yii::$app->user->isGuest ?
['label' => 'Login', 'url' => ['/site/login']] :
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
The not working code:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
Yii::$app->user->isGuest ?
(['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]) :
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
],
],
]);
I understand what the problem is, but can't figure out a workarround for it.
Any ideas?
You have to have have each part of the code evaluate to something legal - this is not legal:
Yii::$app->user->isGuest ?
(['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']])
because it would evaluate to
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
that would only be valid if their were another set of [] around the whole thing - for example, you could not do:
$x = ['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']];
but you could do
$x = [
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
];
Your code needs to be more like
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => Yii::$app->user->isGuest ?
[
['label' => 'Registrieren', 'url' => ['/site/register']],
['label' => 'Login', 'url' => ['/site/login']]
] :
[
[
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
]
]
]);
This line looks wrong:
(['label' => 'Registrieren', 'url' => ['/site/register']]
You have one opening brace '(' but two closing ']' braces, you probably want to remove one of those
I am using the YII2 Menu Widget and did not find the solution to add attribute options like class , target on created link.
My code is below:
echo Menu::widget(
[
'options' => [
'class' => 'sidebar-menu'
],
'items' => [
[
'label' => Yii::t('backend', 'Admin'),
'url' => Yii::$app->homeUrl,
'icon' => 'fa-list-alt',
'options' => [
'class' => 'treeview',
],
'items' => [
[
'label' => Yii::t('backend', 'External link'),
'url' => 'http://google.com',
'icon' => 'fa-list-alt',
'options' => [
'target' => '_blank',
],
],
]
],
]
]
);
Option target is not added on generated link.
add the target like below through the template setting. The Options you have set in your code are the Html Options of the li element and not the link options.
'items' => [
[
'label' => Yii::t('backend', 'External link'),
'url' => 'http://google.com',
'icon' => 'fa-list-alt',
'template'=> '{label}',
],
]
Suggestions above do not seem to be working (in my case), an alternative solution is:
'linkOptions' => ['target' => '_blank']
For example
[
'url' => \Yii::$app->user->identity->getBlogLink(),
'linkOptions' => ['target' => '_blank'] ,
'label' => \Yii::t('app', 'Blog')
]
If you want to keep the icon in your menu:
'template'=> '{icon}{label}'
As regards the class you must specify it in the options key:
[
'label' => 'Debug',
'icon' => 'fa fa-dashboard',
'url' => ['/debug'],
'options' => ['class' => 'special'],
'template'=> '{icon}{label}',
],
gives the following menu:
<li class="special">
<a target="_blank" href="your_site_url_here/index.php?r=debug">
<i class="fa fa-dashboard"></i>
<span>Debug</span>
</a>
</li>
You can add any url. For Example,
echo Menu::widget([
'items' => [
['label' => 'Home', 'url' => ['http://www.google.com']],
['label' => 'About', 'url' => ['site/about']],
['label' => 'Contact', 'url' => ['site/contact']],
],
'options' => [
'class' => 'navbar-nav nav',
'id'=>'navbar-id',
'style'=>'font-size: 14px;',
'data-tag'=>'yii2-menu',
],
]);
I'm trying to put modal in a navbar in my yii2 project. I'm using yii2-bootstrap extension.
Code for my nav:
NavBar::begin([
'brandLabel' => 'My Company',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
],
]
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
$menuItems[] = [
'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>
code for modal:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
]);
echo 'Say hello...';
Modal::end();
?>
can anyone please tell me how to add this modal to navbar?
First place the modal with an id on your site/index
<?php
use yii\bootstrap\Modal;
Modal::begin(['id' => 'modal',
'header' => '<h2>Hello world</h2>']);
echo "Say Hello...";
Modal::end();
?>
Then create a jQuery action in your controller/SiteController
function actionShowmodal(){
$js='$("#modal").modal("show")';
$this->getView()->registerJs($js);
return $this->render('index');
}
Finally in views\layouts\main add the link in your Nav::wigdet
['label' => 'Show Modal', 'url' => ['/site/showmodal']],
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
//['label' => 'facilities', 'url' => ['/facilities/index']],
['label' => 'Hotel',
'items' => [
['label' => 'Facilities', 'url' => ['/facilities/index']],
// '<li class="divider"></li>',
// '<li class="dropdown-header">Dropdown Header</li>',
['label' => 'Cuisines', 'url' => ['/cuisines/index']],
// insert this line
'<li><a data-toggle="modal" data-target="#modal" style="cursor: pointer;">Click me gently!</a></li>',
],
]
];
And for the modal widget:
<?php
Modal::begin([
'header' => '<h2>Hello world</h2>',
'toggleButton' => ['label' => 'click me'],
'id' => 'modal', // <-- insert this modal's ID
]);
echo 'Say hello...';
Modal::end();
?>