PHP code example of ttlphp / laravel-crbac

1. Go to this page and download the library: Download ttlphp/laravel-crbac 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/ */

    

ttlphp / laravel-crbac example snippets


'Laravel\\Crbac\\' => array($vendorDir . '/laravel-crbac/src'),

$vendorDir . '/laravel-crbac/src/helpers.php',

    'name' => '应用名', // 展示的登录和菜单左侧
    'providers' => [ // laravel-10以内版本使用
        Laravel\Crbac\ServiceProvider::class,
    ]

    return [ // laravel-11及以上版本使用
        Laravel\Crbac\ServiceProvider::class,
    ]

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'admin',
            'remember' => 4320,  // 记住登录时长(分)
            // 范围:0 ~ 50, 0 表示不锁定,默认:3
            'attempt_interval' => 3, // 登录失败尝试锁定间隔次数,达到将限时不可登录
            // 范围:0 ~ 120, 0 表示登录失败立即禁用账号,默认:12
            'attempt_max' => 5 // 最多尝试次数,超出将禁用账号
            // 范围:60 ~ 86400,默认:300
            'lock_time' => 300 // 锁定时长基数(秒),实际锁定时长 = lock_time * pow(2, attempt_total / attempt_interval)
        ],
    ],
    'providers' => [
        'admin' => [
            'driver' => 'eloquent',
            'model' => Laravel\Crbac\Models\Power\Admin::class,
        ],
        // 这里可配置其它授权模型
    ],

'locale' => 'cn',

// 必需要有登录数据,否则永远返回false
// 此函数是所有权限验证通用函数
isPower($code, $default = false):bool



// 添加路由,在外层也可以增加 Route::group()
// 使用此路由可生效三个注解功能: @middleware(参数)、@methods(参数)
path_route('mvc-route', '/{controller}/{action}.html', 'App\\Http\\Controllers');


namespace App\Http\Controllers;

use Laravel\Crbac\Controllers\Controller;

/**
 * 追加该控制器全部中间件
 * @middleware(auth)
 */
class TestController extends Controller {
    /**
     * @methods(GET)
     * @middleware(auth)
     */
    public function testMenu(){

    }
    /**
     * @methods(GET, POST)
     */
    public function testItem(){

    }
}


namespace App\Http\Controllers;

use Laravel\Crbac\Controllers\Controller;
use Laravel\Crbac\Annotation\Request\Methods;
use Laravel\Crbac\Annotation\Request\Middleware;

#[@Middleware('auth')]
class TestController extends Controller {

    #[Methods(Methods::GET)]
    #[@Middleware('auth')]
    public function testMenu(){

    }

    #[Methods(Methods::GET, Methods::POST)]
    public function testItem(){

    }
}


// 使用注解:@powerMenu(参数) 和 @powerItem(参数)。

namespace App\Http\Controllers;

use Laravel\Crbac\Controllers\Controller;

class TestController extends Controller {
    /**
     * @powerMenu('测试菜单')
     */
    public function testMenu(){

    }
    /**
     * @powerItem('测试权限项')
     */
    public function testItem(){

    }
}


namespace App\Http\Controllers;

use Laravel\Crbac\Annotation\PowerMenu;
use Laravel\Crbac\Annotation\PowerItem;
use Laravel\Crbac\Controllers\Controller;
use Laravel\Crbac\Annotation\Request\Methods;
use Laravel\Crbac\Annotation\Request\Middleware;

class TestController extends Controller {

    #[PowerMenu('测试菜单')]
    public function testMenu(){

    }

    #[PowerItem('测试权限项')]
    public function testItem(){

    }
}

namespace App\Http\Controllers;

use Laravel\Crbac\Controllers\Controller;

class TestController extends Controller {
    /**
     * @methods(POST)
     */
    public function test(){
        $file = $this->getUploadFile('file'); // 分割与非分割上传均可
        $dir = public_path('static/images');
        $file->move($dir, $filename);
        return prompt(['url' => '/static/images/' . $file->getBasename()]);
    }
}

crbac_config($key, $default);
shell
php artisan crbac:table
shell
php artisan crbac:lang
shell
php artisan crbac:power