PHP code example of mdt-star / nexus

1. Go to this page and download the library: Download mdt-star/nexus 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/ */

    

mdt-star / nexus example snippets


// 自动推断包名(推荐)
Route::auth(function () {
    Route::get('/articles', [ArticleController::class, 'index']);
    Route::post('/articles', [ArticleController::class, 'store']);
});

// 显式指定包名
Route::auth('third-party/module-article', function () {
    Route::get('/articles', [ArticleController::class, 'index']);
});

// 自定义 tag
Route::auth(function () {
    Route::get('/custom', [CustomController::class, 'index'])->tag('custom:tag');
});

// 指定 tag 和 guard
Route::get('/global', ...)->middleware('auth.tag:global:tag,web');

// 回调接收 $route 参数(MountInstance 实例)
Route::mount('api', function ($route) {
    $route->get('/users', [UserController::class, 'index']);
});

// 指定版本
Route::mount('api:v2', function ($route) {
    $route->get('/users', [UserController::class, 'index']);
});

// 等价于 Route::mount('api', ...)
Route::api(function ($route) {
    $route->get('/users', [UserController::class, 'index']);
});

// 在模块的 ServiceProvider 中注册
Route::extendMount('admin', function (string $version = 'v1') {
    return [
        'extends' => "api:{$version}",
        'prefix' => '/admin',
    ];
});

// 使用
Route::mount('admin', function ($route) {
    $route->get('/dashboard', [DashboardController::class, 'index']);
});
// 等价于:/api/v1/admin/dashboard,带 auth 中间件

use MdtStar\Nexus\Scopes\HasDataScope;

class Article extends Model
{
    use HasDataScope;
}

use MdtStar\Nexus\Contracts\HasModelAccess;
use MdtStar\Nexus\Traits\HasModelAccessTrait;

class Article extends Model implements HasModelAccess
{
    use HasModelAccessTrait;
}

use MdtStar\Nexus\Contracts\HasPermission;
use MdtStar\Nexus\Traits\HasPermissionTrait;

class Admin extends Model implements HasPermission
{
    use HasPermissionTrait;
}

// 检查用户是否有指定 tag
$user->hasTag('article:list', $packageId);

// 获取用户所有 tag(含角色穿透)
$tags = $user->getPermissionTags();

// 清理权限缓存
$user->flushPermissionCache();
bash
php artisan vendor:publish --tag=nexus-config
bash
php artisan vendor:publish --tag=nexus-lang
bash
php artisan nexus:sync-permissions --package=third-party/module-article
bash
php vendor/bin/phpunit