您的位置:首页 > 编程语言 > PHP开发

yii2高级模板安装和yii migrate的使用

2016-06-15 12:16 716 查看
1.通过composer 安装高级版

C:wampwwwyii>composer create-project --prefer-dist yiisoft/yii2-app-advanced advanced   

2. 进入 advanced 目录中 执行 init 初始化

[php] view
plain copy

 print?

C:wampwwwyii>cd advanced  

C:wampwwwyiiadvanced>init  

Yii Application Initialization Tool v1.0  

  

Which environment do you want the application to be initialized in?  

  

[0] Development  

[1] Production  

  

Your choice [0-1, or “q” to quit] 0  

  

Initialize the application under ‘Development’ environment? [yes|no] y  

  

Start initialization …  

  

generate backend/config/main-local.php  

generate backend/config/params-local.php  

generate backend/web/index-test.php  

generate backend/web/index.php  

generate common/config/main-local.php  

generate common/config/params-local.php  

generate console/config/main-local.php  

generate console/config/params-local.php  

generate frontend/config/main-local.php  

generate frontend/config/params-local.php  

generate frontend/web/index-test.php  

generate frontend/web/index.php  

generate yii  

generate cookie validation key in backend/config/main-local.php  

generate cookie validation key in frontend/config/main-local.php  

chmod 0777 backend/runtime  

chmod 0777 backend/web/assets  

chmod 0777 frontend/runtime  

chmod 0777 frontend/web/assets  

chmod 0755 yii  

  

… initialization completed.  

windows环境下直接运行根目录下的init.bat命令。

3.到此为止我们配置好了 数据库联系信息 以及数据库,但是我们还没有表,我们使用 yii migrate 命令

[php] view
plain copy

 print?

C:wampwwwyiiadvanced>yii migrate  

Yii Migration Tool (based on Yii v2.0.0)  

Creating migration history table “migration”…done.  

Total 1 new migration to be applied:  

m130524_201442_init  

  

Apply the above migration? (yes|no) [no]:y  

*** applying m130524_201442_init  

> create table {{%user}} … done (time: 0.031s)  

*** applied m130524_201442_init (time: 0.055s)  

  

Migrated up successfully.  

在windows下打卡cmd命令窗口,切换到Yii项目所在目录(包含Yii.bat),就可以在cmd中运行Yii命令了。

使用Yii migrate命令执行sql语句:



也可以在输入./yii查看有哪些命令可用。

直接运行yii migrate会自动找到console/migrations下的*_init.php。

4.RBAC权限

以下代码展示使用 yii\rbac\DbManager 时如何在应用配置文件中配置 
authManager

return [
// ...
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager',
],
// ...
],
];


DbManager
 使用4个数据库表存放它的数据:
yii\rbac\DbManager::$itemTable: 该表存放授权条目(译者注:即角色和权限)。默认表名为 "auth_item" 。
yii\rbac\DbManager::$itemChildTable: 该表存放授权条目的层次关系。默认表名为 "auth_item_child"。
yii\rbac\DbManager::$assignmentTable: 该表存放授权条目对用户的指派情况。默认表名为 "auth_assignment"。
yii\rbac\DbManager::$ruleTable: 该表存放规则。默认表名为 "auth_rule"。

继续之前,你需要在数据库中创建这些表。你可以使用存放在 
@yii/rbac/migrations
 目录中的数据库迁移文件来做这件事(译者注:根据本人经验,最好是将授权数据初始化命令也写到这个
RBAC 数据库迁移文件中):

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


现在可以通过 
\Yii::$app->authManager
 访问 
authManager
 。
5.

如果你的权限层次结构不会发生改变,而且你的用户数是恒定的,你可以通过 
authManager
 提供的 API 创建一个 控制台命令 一次性初始化授权数据:
<?php
namespace app\commands;

use Yii;
use yii\console\Controller;

class RbacController extends Controller
{
public function actionInit()
{
$auth = Yii::$app->authManager;

// 添加 "createPost" 权限
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);

// 添加 "updatePost" 权限
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);

// 添加 "author" 角色并赋予 "createPost" 权限
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);

// 添加 "admin" 角色并赋予 "updatePost"
// 和 "author" 权限
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);

// 为用户指派角色。其中 1 和 2 是由 IdentityInterface::getId() 返回的id (译者注:user表的id)
// 通常在你的 User 模型中实现这个函数。
$auth->assign($author, 2);
$auth->assign($admin, 1);
}
}


在用 
yii rbac/init
 执行了这个命令后,我们将得到下图所示的层次结构:

6.RbacController.php放在basic模板里面的commands文件夹下,放在advanced的console/controllers文件夹下,注意命名空间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yii migrate rbac