说明:
使用了yii-admin和yii-adminlte
1、正常安装yii2,过程请自行百度 2、URL美化设置 在\backend\config\main.php中或\common\config\main-local.php中的components数组中添加如下代码:
'urlManager' => \[
'enablePrettyUrl' => true,
'showScriptName' => false,
'suffix' => '.html',
'rules' => \[
\],
\],
3、在\backend\web\目录中添加.htaccess文件,否则可能会报403错误
)htaccess.rar 4、因为使用了composer来下载yii-admin和yii-adminlte,但国内下载的时候网速真不敢让人恭维,所以需要对composer进行一些设置使用国内的镜像 进入yii目录,执行如下命令:
composer config repo.packagist composer https://packagist.phpcomposer.com
5、使用composer安装yii-adminlte: 进入yii根目录:
composer require dmstr/yii2-adminlte-asset “2.*”
将vendor/dmstr/yii2-adminlte-asset/example-views/yiisoft/yii2-app目录下的layouts和site复制到对应项目的view目录下 6、使用composer安装yii-admin:
composer require mdmsoft/yii2-admin “~2.0”
7、配置数据库,在main.php配置文件中添加如下代码:
'db' => \[
'class' => 'yii\\db\\Connection',
'dsn' => 'mysql:host=localhost;dbname=yiiadmin',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
\],
运行vendor/yiisoft/yii2/rbac/migrations/schema-mysql.sql和vendor\mdmsoft\yii2-admin\migrations\schema-mysql.sql文件中的sql语句创建数据表 8、yii-admin配置: 在main.php中添加如下代码:
components数组外添加如下代码:
‘language’ => ‘zh-CN’,
‘modules’ => [
‘admin’ => [
‘class’ => ‘mdm\admin\Module’,
// ‘layout’ => ‘right-menu’, // it can be ‘@path/to/your/layout’.
‘mainLayout’ => ‘@backend/views/layouts/main.php’,
‘menus’ => [
// ‘assignment’ => [
// ‘label’ => ‘aaa’ // 更改label
// ],
// ‘route’ => null, // 禁用菜单
]
],
],
'aliases' => \[
'@mdm/admin' => '@vendor/mdmsoft/yii2-admin',
\],
'as access' => \[
'class' => 'mdm\\admin\\components\\AccessControl',
'allowActions' => \[
// ‘admin/*’
]
],
components数组内添加如下代码:
//权限配置
‘authManager’ => [
‘class’ => ‘yii\rbac\DbManager’,
],
9、进入\backend\view\layout文件夹下left.php文件: 在section中添加如下代码:
<?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) {
//visible
isset($data\['visible'\]) && $return\['visible'\] = $data\['visible'\];
//icon
isset($data\['icon'\]) && $data\['icon'\] && $return\['icon'\] = $data\['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),
\] );
?>