• laravel8.5与layuiadmin整合(8)


    38.开发角色和权限功能,大部功能,与上面的系统用户增删改查类似
    开发角色,在路由文件中
    /routes/web.php
    添加

    // 角色管理
    Route::group(['prefix' => 'admin','middleware'=>['auth']],function() {
        // 展示角色列表
        Route::get('role',[App\Http\Controllers\Admin\RoleController::class,'index'])
            ->name('admin.role');
        // 获取角色数据
        Route::get('role/data',[App\Http\Controllers\Admin\RoleController::class,'data'])
            ->name('admin.role.data');
        // 添加角色
        Route::get('role/create',[App\Http\Controllers\Admin\RoleController::class,'create'])
            ->name('admin.role.create');
        // 保存添加的角色
        Route::post('role/store',[App\Http\Controllers\Admin\RoleController::class,'store'])
            ->name('admin.role.store');
        //编辑角色
        Route::get('role/{id}/edit',[App\Http\Controllers\Admin\RoleController::class,'edit'])
            ->name('admin.role.edit');
        // 保存编辑
        Route::put('role/{id}/update',[App\Http\Controllers\Admin\RoleController::class,'update'])
            ->name('admin.role.update');
        //修改角色的拥有的权限
        Route::get('role/{id}/permission',[App\Http\Controllers\Admin\RoleController::class,'permission'])
            ->name('admin.role.permission');
        // 保存角色修改的权限
        Route::put('role/{id}/assignPermission',[App\Http\Controllers\Admin\RoleController::class,'assignPermission'])
            ->name('admin.role.assignPermission');
    });

    39. 在 /resources/views/admin/layout.blade.php修改
    <a lay-href="">角色管理</a>

    <a lay-href="{{route('admin.role')}}">角色管理</a>

    40.建立角色控制器
    /app/Http/Controllers/Admin/RoleController.php

    namespace App\Http\Controllers\Admin;
    
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    use Spatie\Permission\Models\Role;
    
    class RoleController extends Controller {
    
        /**
         * 显示角色列表
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
         */
        public function index() {
            return view('admin.role.index');
        }
        
        /**
         * 添加创建新角色
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
         */
        public function create() {
            return view('admin.role.create');
        }
    }

    41.分别添加以上的模板文件
    /resources/views/admin/role/index.blade.php

    @extends('admin.base')
    
    @section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <div class="layui-btn-group">
    
                <a class="layui-btn layui-btn-sm" href="{{ route('admin.role.create') }}">添 加</a>
    
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
    
                    <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
    
    
                    <a class="layui-btn layui-btn-sm" lay-event="permission">权限</a>
    
    
                    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</a>
    
                </div>
            </script>
        </div>
    </div>
    @endsection
    
    @section('script')
    <script>
        layui.use(['layer','table','form'],function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 500
                ,url: "{{ route('admin.role.data') }}" //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {checkbox: true,fixed: true}
                    ,{field: 'id', title: 'ID', sort: true,80}
                    ,{field: 'name', title: '角色名称'}
                    ,{field: 'created_at', title: '创建时间'}
                    ,{field: 'updated_at', title: '更新时间'}
                    ,{fixed: 'right',  260, align:'center', toolbar: '#options'}
                ]]
            });
    
            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注:tool是工具条事件名,dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'del'){
                    layer.confirm('确认删除吗?', function(index){
                        $.post("{{ route('admin.role.destroy') }}",{_method:'delete',ids:[data.id]},function (result) {
                            if (result.code==0){
                                obj.del(); //删除对应行(tr)的DOM结构
                            }
                            layer.close(index);
                            layer.msg(result.msg,{icon:6})
                        });
                    });
                } else if(layEvent === 'edit'){
                    location.href = '/admin/role/'+data.id+'/edit';
                } else if (layEvent === 'permission'){
                    location.href = '/admin/role/'+data.id+'/permission';
                }
            });
    
    
        })
    </script>
    @endsection
    
    
    /resources/views/admin/role_form.blade.php
    
    {{csrf_field()}}
    <div class="layui-form-item">
        <label for="" class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input class="layui-input" type="text" name="name" lay-verify="required" value="{{$role->name??old('name')}}" placeholder="如:admin">
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" >确 认</button>
            <a href="{{route('admin.role')}}" class="layui-btn" >返 回</a>
        </div>
    </div>
    
    /resources/views/admin/role/create.blade.php
    
    @extends('admin.base')
    
    @section('content')
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <h2>添加角色</h2>
            </div>
            <div class="layui-card-body">
                <form action="{{route('admin.role.store')}}" method="post" class="layui-form">
                    @include('admin.role._form')
                </form>
            </div>
        </div>
    @endsection

    42.控制器添加获取数据方法
    /app/Http/Controllers/Admin/RoleController.php

        /**
         * 获取角色内容
         * @param Request $request
         * @return \Illuminate\Http\JsonResponse
         */
        public function data(Request $request) {
            $sql = "select * from roles";
            $sqlCount = "select count(*) as total from roles";
            $page = 1;
            $pagesize = 10;
            if($request->get('page')){
                $page = intval($request->get('page'));
                if($page < 1){
                    $page = 1;
                }
            }
    
            if($request->get('limit')){
                $pagesize = $request->get('limit');
                if($pagesize < 1) {
                    $pagesize = 10;
                }
            }
            $start_num = ($page - 1) * $pagesize;
            $sql .= " limit {$start_num},{$pagesize}";
            $listResult= DB::select($sql);
            $totalCount= DB::selectOne($sqlCount);
            $listData = [];
            foreach ($listResult as $data_row) {
                $listData[] = $data_row;
            }
            $data = [
                'code' => 0,
                'msg' => '正在请求中...',
                'count' => $totalCount->total,
                'data' => $listData
            ];
            return response()->json($data);
        }

    此时角色浏览,添加了

  • 相关阅读:
    c++ 11 thread 初试
    java UDP聊天与文件传输
    iOS 基础类解析
    Hadoop HA高可用集群搭建(2.7.2)
    object-c 不定參数的遍历和原理
    9.4 返回更新后的行
    java面向接口编程
    Node.js开发入门—套接字(socket)编程
    shell脚本输出带颜色字体
    shell--read命令
  • 原文地址:https://www.cnblogs.com/lin3615/p/15802987.html
Copyright © 2020-2023  润新知