用ThinkPHP6帮我写一个后台权限分配
时间: 2023-06-27 19:07:58 浏览: 143
好的,以下是一个简单的 ThinkPHP6 后台权限分配示例。
1. 创建一个名为 `Admin` 的控制器,并添加以下代码:
```php
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Role as RoleModel;
use app\admin\model\Permission as PermissionModel;
class Admin extends Base
{
public function index()
{
$adminList = AdminModel::select();
View::assign('adminList', $adminList);
return View::fetch();
}
public function add()
{
$roleList = RoleModel::select();
View::assign('roleList', $roleList);
return View::fetch();
}
public function save()
{
$data = $this->request->post();
$admin = new AdminModel();
$admin->username = $data['username'];
$admin->password = md5($data['password']);
$admin->role_id = $data['role_id'];
$admin->save();
return redirect(url('index'));
}
public function edit($id)
{
$admin = AdminModel::get($id);
$roleList = RoleModel::select();
View::assign('admin', $admin);
View::assign('roleList', $roleList);
return View::fetch();
}
public function update($id)
{
$data = $this->request->post();
$admin = AdminModel::get($id);
$admin->username = $data['username'];
$admin->role_id = $data['role_id'];
if ($data['password']) {
$admin->password = md5($data['password']);
}
$admin->save();
return redirect(url('index'));
}
public function delete($id)
{
AdminModel::destroy($id);
return redirect(url('index'));
}
}
```
2. 创建一个名为 `Role` 的控制器,并添加以下代码:
```php
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\Role as RoleModel;
use app\admin\model\Permission as PermissionModel;
class Role extends Base
{
public function index()
{
$roleList = RoleModel::select();
View::assign('roleList', $roleList);
return View::fetch();
}
public function add()
{
$permissionList = PermissionModel::select();
View::assign('permissionList', $permissionList);
return View::fetch();
}
public function save()
{
$data = $this->request->post();
$role = new RoleModel();
$role->name = $data['name'];
$role->save();
$role->permissions()->saveAll($data['permissions']);
return redirect(url('index'));
}
public function edit($id)
{
$role = RoleModel::get($id);
$permissionList = PermissionModel::select();
View::assign('role', $role);
View::assign('permissionList', $permissionList);
return View::fetch();
}
public function update($id)
{
$data = $this->request->post();
$role = RoleModel::get($id);
$role->name = $data['name'];
$role->permissions()->detach();
$role->permissions()->saveAll($data['permissions']);
$role->save();
return redirect(url('index'));
}
public function delete($id)
{
RoleModel::destroy($id);
return redirect(url('index'));
}
}
```
3. 创建一个名为 `Permission` 的控制器,并添加以下代码:
```php
namespace app\admin\controller;
use think\facade\View;
use app\admin\model\Permission as PermissionModel;
class Permission extends Base
{
public function index()
{
$permissionList = PermissionModel::select();
View::assign('permissionList', $permissionList);
return View::fetch();
}
public function add()
{
return View::fetch();
}
public function save()
{
$data = $this->request->post();
$permission = new PermissionModel();
$permission->name = $data['name'];
$permission->save();
return redirect(url('index'));
}
public function edit($id)
{
$permission = PermissionModel::get($id);
View::assign('permission', $permission);
return View::fetch();
}
public function update($id)
{
$data = $this->request->post();
$permission = PermissionModel::get($id);
$permission->name = $data['name'];
$permission->save();
return redirect(url('index'));
}
public function delete($id)
{
PermissionModel::destroy($id);
return redirect(url('index'));
}
}
```
4. 创建一个名为 `Base` 的控制器作为其他控制器的父类,并添加以下代码:
```php
namespace app\admin\controller;
use think\Controller;
use think\facade\Session;
use app\admin\model\Admin as AdminModel;
class Base extends Controller
{
protected function initialize()
{
parent::initialize();
$this->checkLogin();
$this->checkPermission();
}
protected function checkLogin()
{
if (!Session::get('admin_id')) {
$this->redirect(url('login/index'));
}
}
protected function checkPermission()
{
$admin = AdminModel::get(Session::get('admin_id'));
if (!$admin->role->hasPermission(request()->controller(), request()->action())) {
$this->error('您没有权限访问该页面!');
}
}
}
```
5. 创建三个模型分别为 `Admin`、`Role` 和 `Permission`,并添加以下代码:
```php
namespace app\admin\model;
use think\Model;
class Admin extends Model
{
public function role()
{
return $this->belongsTo('Role');
}
}
class Role extends Model
{
public function permissions()
{
return $this->belongsToMany('Permission');
}
public function hasPermission($controller, $action)
{
foreach ($this->permissions as $permission) {
if ($permission->controller == $controller && $permission->action == $action) {
return true;
}
}
return false;
}
}
class Permission extends Model
{
public function roles()
{
return $this->belongsToMany('Role');
}
}
```
6. 创建三个数据表分别为 `admin`、`role` 和 `permission`,并添加以下字段:
```sql
CREATE TABLE `admin` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL DEFAULT '',
`password` varchar(50) NOT NULL DEFAULT '',
`role_id` int(11) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `role` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `permission` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL DEFAULT '',
`controller` varchar(50) NOT NULL DEFAULT '',
`action` varchar(50) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE `role_permission` (
`role_id` int(11) unsigned NOT NULL,
`permission_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`role_id`,`permission_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `admin` ADD CONSTRAINT `admin_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `role_permission` ADD CONSTRAINT `role_permission_role` FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `role_permission` ADD CONSTRAINT `role_permission_permission` FOREIGN KEY (`permission_id`) REFERENCES `permission` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
```
7. 最后,在 `config/router.php` 文件中添加以下路由:
```php
use think\facade\Route;
Route::group('admin', function () {
Route::get('login', 'Login/index');
Route::post('login', 'Login/login');
Route::get('logout', 'Login/logout');
Route::get('admin', 'Admin/index');
Route::get('admin/add', 'Admin/add');
Route::post('admin/save', 'Admin/save');
Route::get('admin/edit/:id', 'Admin/edit')->pattern(['id' => '\d+']);
Route::post('admin/update/:id', 'Admin/update')->pattern(['id' => '\d+']);
Route::get('admin/delete/:id', 'Admin/delete')->pattern(['id' => '\d+']);
Route::get('role', 'Role/index');
Route::get('role/add', 'Role/add');
Route::post('role/save', 'Role/save');
Route::get('role/edit/:id', 'Role/edit')->pattern(['id' => '\d+']);
Route::post('role/update/:id', 'Role/update')->pattern(['id' => '\d+']);
Route::get('role/delete/:id', 'Role/delete')->pattern(['id' => '\d+']);
Route::get('permission', 'Permission/index');
Route::get('permission/add', 'Permission/add');
Route::post('permission/save', 'Permission/save');
Route::get('permission/edit/:id', 'Permission/edit')->pattern(['id' => '\d+']);
Route::post('permission/update/:id', 'Permission/update')->pattern(['id' => '\d+']);
Route::get('permission/delete/:id', 'Permission/delete')->pattern(['id' => '\d+']);
})->middleware(\app\admin\middleware\Auth::class);
```
其中,`Auth` 中间件用于检查用户是否登录。如果未登录,将重定向到登录页面。
至此,一个简单的 ThinkPHP6 后台权限分配示例就完成了。
阅读全文