PHP code example of ezijing-mse-hyperf / permission

1. Go to this page and download the library: Download ezijing-mse-hyperf/permission library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ezijing-mse-hyperf / permission example snippets


        $roles = Role::with(['users', 'perms'])->get();
        foreach ($roles as $role) {
            if ($role->perms) {
                foreach ($role->perms as $perm) {
                    Enforcer::addPermissionForUser($role->name, $perm->name, 'ANY');
                }
            }
            if ($role->users) {
                foreach ($role->users as $user) {
                    Enforcer::addRoleForUser($user->username, $role->name);
                }
            }
        }

   php bin/hyperf.php permission:cache-reset
   

   $user->getMenu(); // 获取当前登录用户的菜单,即url有值。
    /**
     * 获取树形的permission列表.
     * @param int||string $parentId 父级ID
     * @param bool $isUrl 是否是一个URL
     * @param Collection $permission 传入permission集合,如果不传将从所有的permission生成
     * @return Collection
     */
   Permission::getMenuList($parentId = 0, $isUrl = false, Collection $permission = null);
   

 php bin/hyperf.php vendor:publish ezijing-mse-hyperf/permission

php bin/hyperf.php migrate