[关闭]
@liuwanwei 2017-05-15T10:47:28.000000Z 字数 2188 阅读 1637

Yii2-admin 从零开始

Yii


1. 安装

composer require mdmsoft/yii2-admin "~2.0"

准备工作

为了确保后序执行数据迁移(migrate)成功,需要进行一些准备工作。

  1. 'db' => [
  2. 'class' => 'yii\db\Connection',
  3. 'dsn' => 'mysql:host=127.0.0.1;dbname=shmetro',
  4. 'username' => 'root',
  5. 'password' => '1801379',
  6. 'charset' => 'utf8',
  7. ],

导入 menu manager

Menu Manager 会根据配置的用户权限生成对应菜单,一般为必选项。

  1. ./yii migrate --migrationPath=@mdm/admin/migrations

导入后就会创建

修改 backend\config\main.php

  1. // 引入 yii2-admin 子模块
  2. 'admin' => [
  3. 'class' => 'mdm\admin\Module',
  4. 'layout' => 'left-menu',
  5. // 指定 layout,跟项目保持一致
  6. 'mainLayout' => '@backend/views/layouts/main.php',
  7. ],
  1. // 使用本地文件作为权限控制对象存储位置
  2. // 务必确保 backend/rbac 目录存在,且服务器拥有读写权限
  3. 'authManager' => [
  4. 'class' => 'yii\rbac\PhpManager',
  5. ],
  1. // 添加下行,指定登录 url 地址
  2. 'user' => [
  3. ...
  4. 'loginUrl' => ['/admin/user/login'],
  5. ...
  6. ],
  1. 'as access' => [
  2. 'class' => 'mdm\admin\components\AccessControl',
  3. 'allowActions' => [
  4. 'site/*',
  5. // 配置完成后注释掉下行
  6. 'admin/*',
  7. ],
  8. ],

经以上基础配置,就可以通过访问 admin 模块:

http://localhost/path/to/index.php?r=admin
http://localhost/path/to/index.php?r=admin/route
http://localhost/path/to/index.php?r=admin/permission
http://localhost/path/to/index.php?r=admin/menu
http://localhost/path/to/index.php?r=admin/role
http://localhost/path/to/index.php?r=admin/assignment
http://localhost/path/to/index.php?r=admin/user

创建默认用户

通过 yii2-admin 默认提供的 signup 界面注册用户。

2. 主题配置

2.1 使用 yii2-bootswatch-asset

安装:

  1. require --prefer-dist raoul2000/yii2-bootswatch-asset "*"

导入资源:

  1. // 编辑:backend/assets/AppAsset.php
  2. class AppAsset extends AssetBundle
  3. {
  4. public $basePath = '@webroot';
  5. public $baseUrl = '@web';
  6. public $css = [
  7. 'css/site.css',
  8. ];
  9. public $js = [
  10. ];
  11. public $depends = [
  12. 'yii\web\YiiAsset',
  13. // 主要就这一行
  14. 'raoul2000\bootswatch\BootswatchAsset',
  15. ];
  16. }

使用资源:

  1. // 编辑:backend/views/layouts/main.php
  2. raoul2000\bootswatch\BootswatchAsset::$theme = 'cosmo';
  3. AppAsset::register($this);

2.2 使用 yii2-adminlte-asset

TODO:

3. 小知识

PhpManager 还是 DbManager

在配置 authManager 时,除了 PhpManager 外,还可以选择 DbManager。他俩是 rbac 数据的存储方式,二选一,如果想在数据库中保存,需要执行:

  1. ./yii migrate --migrationPath=@yii/rbac/migrations

执行后,会在数据库中创建下面几个表:

  1. rule
  2. item
  3. item_child
  4. assignment

但是问题来了:选择哪种保存方式呢?

PhpManager:

DbManager:

从以上两个方式特点来看,如果你不怕麻烦,或者由于项目特点,可以经常导入导出数据库的话,建议选择 DbManager,否则还是选择 PhpManager。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注