$nav = [
'items' => Yii::$app->user->isGuest ? '' : (
['label' => 'Главная', 'url' => ['/site/index']]
),
Yii::$app->user->isGuest ? "" : (
['label' => 'Сотрудники', 'url' => ['/site/about']]),
Yii::$app->user->isGuest ? "" : (
['label' => 'Лиды', 'url' => ['/site/contact']]),
Yii::$app->user->isGuest ? "" : (
['label' => 'Заказы', 'url' => ['/site/contact']]),
Yii::$app->user->isGuest ? "" : (
['label' => 'Настройки', 'url' => ['/site/contact']]),
];
if (Yii::$app->user->isGuest) {
$nav = [
'items' => [],
];
} else {
$nav = [
'items' => [
['label' => 'Главная', 'url' => ['/site/index']] ,
['label' => 'Сотрудники', 'url' => ['/site/about']],
['label' => 'Лиды', 'url' => ['/site/contact']],
['label' => 'Заказы', 'url' => ['/site/contact']],
['label' => 'Настройки', 'url' => ['/site/contact']],
],
];
}
<?php
NavBar::begin([
'brandLabel' => Yii::$app->name,
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-default navbar-fixed-top',
],
]);
$menuItems = [
['label' => 'Обо мне', 'url' => ['/site/about']],
['label' => 'Контакты', 'url' => ['/site/contact']],
];
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Регистрация', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Вход', 'url' => ['/site/login']];
} else {
$menuItems[] = ['label' => 'Выйти (' . 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();
?>
public function validatePhone($attribute, $params)
{
if (!$this->hasErrors()) {
$user = User::findOne(['phone' => $this->$attribute]);
if ($user ) {
$this->addError($attribute, 'Телефон занят');
}
}
['phone', 'validatePhone'],
Установил на хостинг битрикс.
3.У меня простой однастраничник в который нужно добавить пару инфоблоков
$authManager = Yii::$app->authManager;
$authManager->removeAll();
$guestSignUp = $authManager->createPermission('guest-sign-up');
$authManager->add($guestSignUp);
$userRoleRule = new UserRoleRule();
$authManager->add($userRoleRule);
$guest = $authManager->createRole('guest');
$authManager->add($guest);
$authManager->addChild($guest, $guestSignUp);
if (\Yii::$app->user->can('guest-sign-up')) {
echo Html::a('sign up', ['/sign-up']);
}
$authManager->addChild($guest, $guestSignUp);
и всё.<?php
namespace common\components\rbac;
use common\modules\user\models\User;
use yii;
use yii\rbac\Item;
use yii\rbac\Rule;
/**
* Class UserRoleRule
* @package common\components\rbac
*/
class UserRoleRule extends Rule
{
public $name = 'userRoleRule';
public function execute($user, $item, $params)
{
$role = Yii::$app->user->isGuest ? User::ROLE_GUEST : User::identity()->role;
if ($item->name === 'developer') {
return $role == User::ROLE_DEVELOPER;
} elseif ($item->name === 'admin') {
return $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'manager') {
return $role == User::ROLE_MANAGER || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'author') {
return $role == User::ROLE_AUTHOR || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'user') {
return $role == User::ROLE_USER || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'guest') {
return $role == User::ROLE_GUEST || $role >= User::ROLE_ADMIN;
}
return false;
}
}