Make a simple sub-menu using HTML, PHP - php

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

Related

Yii2 How to add a new temporary item to NavBar items using isGuest

I am new to Yii2 and recently downloaded a basic-app version of the framework.
There is a code snippet in the "main.php" file:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>'
)
]
]);
where I want to add registration link like this:
Yii::$app->user->isGuest ? (
['label' => 'Login', 'url' => ['/user/login']],
['label' => 'Register', 'url' => ['/user/registration']]
) : (
'<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
I know this peace of code is wrong, but I want to ask, how can I do this in right way?
Your attempt does not work, because you use a ternary operator which allows only for 1 value and not 2 values. Login and Registration are 2 arrays.
A possible solution:
// What everybody sees
$items = [
['label' => 'Main', 'url' => ['/site/index']],
['label' => 'Countries', 'url' => ['/country/countries']],
['label' => 'About us', 'url' => ['/site/about']],
['label' => 'Contacts', 'url' => ['/site/contact']],
];
// What only guests see
if (Yii::$app->user->isGuest) {
$items[] = ['label' => 'Registration', 'url' => ['/user/registration']];
$items[] = ['label' => 'Login', 'url' => ['/user/login']];
}
// What logged in users see
else {
$items[] = '<li>'
. Html::beginForm(['/user/logout'], 'post')
. Html::submitButton(
'Logout(' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>';
}
// Echo your navigation
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $items,
]);

Error Class 'kartik\widgets\SideNav' not found

I have downloaded yii2-widgets-master and don't know where I should keep it.
I am facing the error Class kartik\widgets\SideNav not found.
My code is here
<?php
use yii\helpers\Url;
use kartik\widgets\SideNav;
?>
<div id = "links">
<div class="user-img"><i class="fa fa-user" aria- hidden="true"></i>
<div class="text-center info"> <p>Wecare</p></div>
</div>
<?php
$menuItems[] = ['label' => 'DFenX - Yii2 User - '. Yii::t('app','User Admin Panel'), 'icon' => 'cog', 'url'=>Url::to(['/user/admin/index'])];
$menuItems[] = ['label' => Yii::t('app','Authentication manager'), 'icon' => 'th-list', 'items' => [
['label' => 'Settings', 'icon' => 'th-list', 'items' => [
['label' => '/user/settings', 'url'=>Url::to(['/user/settings'])],
['label' => '/user/settings/profile', 'url'=>Url::to(['/user/settings/profile'])],
['label' => '/user/settings/account', 'url'=>Url::to(['user/settings/account'])],
['label' => '/user/settings/networks', 'url'=>Url::to(['/user/settings/networks'])],
]],
['label' => 'Registration', 'icon' => 'th-list', 'items' => [
['label' => '/user/registration/register', 'url'=>Url::to(['/user/registration/register'])],
['label' => '/user/registration/resend', 'url'=>Url::to(['/user/registration/resend'])],
]],
['label' => 'Security', 'icon' => 'th-list', 'items' => [
['label' => '/user/security/login', 'url'=>Url::to(['/user/security/login'])],
['label' => '/user/security/logout', 'url'=>Url::to(['/user/security/logout'])],
]],
['label' => 'Recovery', 'icon' => 'th-list', 'items' => [
['label' => '/user/recovery/request', 'url'=>Url::to(['/user/recovery/request'])],
['label' => '/user/recovery/reset', 'url'=>Url::to(['/user/recovery/reset'])],
]],
]];
$type = SideNav::TYPE_PRIMARY;
$heading = '<i class="glyphicon glyphicon-user"></i> ' . Yii::t('app','USER Admin - UTILITIES');
echo SideNav::widget([
'type' => $type,
'encodeLabels' => false,
'heading' => $heading,
'items' =>$menuItems,
]);
?>
</div>
Add to composer.json:
"kartik-v/yii2-widget-sidenav": "*"
Run composer update in console under project directory.

Visibility in Yii2

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'],
],
]);
}

Yii2 Menu Bar Visibility to a Specific User

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

how can I add modal to navbar in yii2 using yii2 -bootstrap extension?

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

Categories