Skip to content
关注公众号,获取新课通知

角色列表


控制器 app/controller/admin/Role.php

php
public function index()
{
	$param = request()->param();
	$limit = intval(getValByKey('limit',$param,10));
	$totalCount = $this->M->count();
	$list = $this->M->with(['rules'=>function($q){
		$q->alias('a')->field('a.id');
	}])->page($param['page'],$limit)->order(['id'=>'desc'])->select();
	return showSuccess([
		'list'=>$list,
		'totalCount'=>$totalCount
	]);
}

模型关联

php
// app/model/Role.php
// 当前角色的所有权限
public function rules(){
	return $this->belongsToMany('Rule','role_rule');
}
// app/model/Rule.php
// 角色-规则多对多关系
public function roles(){
	return $this->belongsToMany('Role','role_rule');
}

路由 router/admin.php

php
Route::get('role/:page','admin.Role/index');

验证器 app/validate/admin/Role.php

php
protected $rule = [
	'page' => 'require|integer|>:0',
];
protected $scene = [
	...
	'index'=>['page'],
	...
];