使用yii2的advance版本進行快速建站(rbac控制)
安裝yii2 和 一些yii2的插件
建議使用composer方式進行操作
下載yii
composer create-project --prefer-dist yiisoft/yii2-app-advanced honglvshi.cn
cd honglvshi.cn
php init
/*會出現兩個選項 讓你選擇是生產環境 還是 開發環境 */
配置nginx
/*前臺nginx配置*/
server{
listen 80;
server_name ******;
root /data/www/develop/******/frontend/web;
index index.php index.html;
access_log /data/log/nginx/develop/******.access.log;
error_log /data/log/nginx/develop/******.error.log;
include php-fpm.conf;
}
/*後臺nginx配置*/
server{
listen 80;
server_name ******;
root /data/www/develop/******/backend/web;
index index.php index.html;
access_log /data/log/nginx/develop/******.access.log;
error_log /data/log/nginx/******.error.log;
include php-fpm.conf;
}
複製的時候注意刪除註釋 路徑需要自己根據自己服務器進行配置
安裝yii2 拓展
/*後臺adminlte 響應式模版*/
composer require dmstr/yii2-adminlte-asset "2.*"
/*yii2-admin 官方可視化rbac管理組件*/
composer require mdmsoft/yii2-admin "2.x-dev"
修改yii2項目
/* 將adminlte的layout佈局替換yii2原始的layout佈局*/
cp -r yii2-app/* /data/www/develop/honglvshi.cn/backend/views/
/* 修改yii2 backend 的配置項*/
/*增加以下兩個組件*/
'components' => [
/*rbac 角色授權管理*/
"authManager" => [
"class" => 'yii\rbac\DbManager',
"defaultRoles" => ["guest"],
],
/* url美化 */
"urlManager" => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=*****',
'username' => '***',
'password' => '****',
'charset' => 'utf8',
]
],
/*模塊增加以下代碼 以後每增加一個模塊 都需要再以下配置增加*/
"modules" => [
"admin" => [
"class" => "mdm\admin\Module",
],
],
/**/
"aliases" => [
"@mdm/admin" => "@vendor/mdmsoft/yii2-admin",
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
//目前允許訪問所有action 強烈建議填寫 登錄 註銷 和異常頁面
'*'
]
],
創建rbac的數據庫
/*sql語句存在/vendor/yiisoft/yii2/rbac/migrations/shcema-mysql.sql*/
create table `auth_rule`
(
`name` varchar(64) not null,
`data` blob,
`created_at` integer,
`updated_at` integer,
primary key (`name`)
) engine InnoDB;
create table `auth_item`
(
`name` varchar(64) not null,
`type` smallint not null,
`description` text,
`rule_name` varchar(64),
`data` blob,
`created_at` integer,
`updated_at` integer,
primary key (`name`),
foreign key (`rule_name`) references `auth_rule` (`name`) on delete set null on update cascade,
key `type` (`type`)
) engine InnoDB;
create table `auth_item_child`
(
`parent` varchar(64) not null,
`child` varchar(64) not null,
primary key (`parent`, `child`),
foreign key (`parent`) references `auth_item` (`name`) on delete cascade on update cascade,
foreign key (`child`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
create table `auth_assignment`
(
`item_name` varchar(64) not null,
`user_id` varchar(64) not null,
`created_at` integer,
primary key (`item_name`, `user_id`),
foreign key (`item_name`) references `auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
CREATE TABLE `menu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
`parent` int(11) DEFAULT NULL,
`route` varchar(256) DEFAULT NULL,
`order` int(11) DEFAULT NULL,
`data` text,
PRIMARY KEY (`id`),
KEY `parent` (`parent`),
CONSTRAINT `menu_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `menu` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`auth_key` varchar(32) NOT NULL,
`password_hash` varchar(255) NOT NULL,
`password_reset_token` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`role` smallint(6) NOT NULL DEFAULT '10',
`status` smallint(6) NOT NULL DEFAULT '10',
`created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
進入http://admin.**/admin/route查看效果
大功造成了一半 嗯 繼續往下
修改佈局的左邊菜單欄
<aside class="main-sidebar">
<section class="sidebar">
<div class="user-panel">
<div class="pull-left image">
<img src="<?= $directoryAsset ?>/img/user2-160x160.jpg" class="img-circle" alt="User Image"/>
</div>
<div class="pull-left info">
<p>Alexander Pierce</p>
<a href="#"><i class="fa fa-circle text-success"></i> Online</a>
</div>
</div>
<form action="#" method="get" class="sidebar-form">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..."/>
<span class="input-group-btn">
<button type='submit' name='search' id='search-btn' class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
<?php
use mdm\admin\components\MenuHelper;
$callback = function($menu){
$data = json_decode($menu['data'], true);
$items = $menu['children'];
$return = ['label' => $menu['name'],'url' => [$menu['route']]];
//處理我們的配置
if ($data) {
isset($data['visible']) && $return['visible'] = $data['visible'];//visible
isset($data['icon']) && $data['icon'] && $return['icon'] = $data['icon'];//icon
//other attribute e.g. class...
$return['options'] = $data;
}
//沒配置圖標的顯示默認圖標
(!isset($return['icon']) || !$return['icon']) && $return['icon'] = 'fa fa-circle-o';
$items && $return['items'] = $items;
return $return;
};
//這裏我們對一開始寫的菜單menu進行了優化
echo dmstr\widgets\Menu::widget( [
'options' => ['class' => 'sidebar-menu'],
'items' => MenuHelper::getAssignedMenu(Yii::$app->user->id,null, $callback),
] );
?>
</section>
</aside>
創建一個用戶
在frontend的config/main.php 同樣配置數據庫
'components' => [
/*rbac 角色授權管理*/
/* url美化 */
"urlManager" => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=*****',
'username' => '***',
'password' => '****',
'charset' => 'utf8',
]
],
/*前臺/site/signup 註冊一個帳號 就可以再後臺登錄了*/
/admin/route rbac的路由
/admin/permission rbac的權限
/admin/role rbac的角色
/admin/assigment rbac的分配權限
/admin/menu rbac的菜單
先配置超級管理員的權限
進入****/admin/role/index
選中超級管理員
分配route給 /*
然後進入****/admin/assigment/index
分配超級管理員給我們剛創建的用戶
1.先創建路由
2.在對路由創建權限(強烈建議對路由進行一對一分配權限)
3.在對權限一對多創建權限
4.在對角色分配權限
5.然後創建菜單
接下里可以根據用戶和自己的模塊進行rbac的分配了 如果不知道rbac是什麼 最好百度或者google瞭解下rbac的系統
項目的前臺在frontend進行編寫代碼
項目的後臺在backend進行編寫代碼
再根據自己的其他需求自行選擇compose框架
yii2 的advanced版本已經涵蓋很多功能 基本你想要的yii2 advanced版本都涵蓋了