reivaj86 / multiapps
一个简单的Laravel 5软件包,用于处理主应用程序中的子应用程序。
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-28 17:02:14 UTC
README
这是一个简单的Laravel 5软件包,用于处理主应用程序中的子应用程序(Appls)。每个Appl可以看作是主APP中的一个模块或包。使用此包,您可以授予或撤销对每个应用程序的访问权限,将appls附加到用户或从用户中移除。
Main App. //with 2 child apps
-->Appl_1. (Module 1)
-->Appl_2. (Module 2)
.....
...
安装
通过Composer拉取此包。---- composer.json
{ "require": { "reivaj86/multiapps": "dev-master" } }
运行 $ composer update
将包添加到您的应用程序服务提供者中: config/app.php
'providers' => [ 'Illuminate\Foundation\Providers\ArtisanServiceProvider', 'Illuminate\Auth\AuthServiceProvider', ... 'Reivaj86\Multiapps\MultiappsServiceProvider', ],
发布包迁移。将配置文件发布到您的应用程序。
$ php artisan vendor:publish --provider="Vendor/Reivaj86/Multiapps/MultiappsServiceProvider" --tag="config"
$ php artisan vendor:publish --provider="Vendor/Reivaj86/Multiapps/MultiappsServiceProvider" --tag="migrations"
运行迁移。
$ php artisan migrate
创建种子 // 可选:运行生成器
配置文件 ---- config.php
您可以更改模型连接、slug分隔符,还有一个非常有用的用途选项。查看配置文件以获取更多信息。
使用 ---- IsApplUser 特性 & IsApplUserContract
首先,包含 IsApplUser 特性,并在您的 User 模型或 Custom 模型中实现 IsApplUserContract。
use Reivaj86\Multiapps\Contracts\IsApplUserContract; use Reivaj86\Multiapps\Traits\IsApplUser; class User extends Model implements AuthenticatableContract, CanResetPasswordContract, IsApplUserContract { use Authenticatable, CanResetPassword, IsUserAppl;
完成!您可以为用户创建第一个Appl并将其附加到用户或Custom模型。
use Reivaj86\Multiapps\Models\Appl; use App\User; $appl = Appl::create([ 'name' => 'Child_App_Name', 'slug' => 'child_app_slug', 'description' => '' // optional ]); $user = User::find($id)->attachAppl($appl); // you can pass whole object, or just id
您可以轻松检查当前用户是否使用子应用程序。
if ($user->uses('child_app')) // you can pass an id or slug { return 'child_app_slug'; }
您还可以进行以下操作
if ($user->usesChild_App_Name()) { return 'child_app_slug'; }
此外,还有方法检查用户/模型是否可以访问多个Appl
if ($user->can('child_app_1|child_app_2')) // or $user->can('child_app_1, child_app_2') and also $user->can(['child_app_1', 'child_app_2']) { // if user has at least one appl } if ($user->can('child_app_1|child_app_2', 'All')) // or $user->can('child_app_1, child_app_2', 'All') and also $user->can(['child_app_1', 'child_app_2'], 'All') { // if user has all appls }
在创建Appl时,还有一个可选的参数 level。默认设置为 1,但您可以覆盖它,然后您可以这样做
if ($user->level() > 3) { // code }
此选项非常有用,当您想为每个子应用程序设置访问级别时。您可以轻松将其与角色和级别身份验证包集成。如果用户有多个子应用程序,方法 level 返回该应用程序的级别。对于基本用户,级别始终为1。
Blade 扩展 ---- @appl & @uses & @allowedappl
有三个Blade扩展。基本上,它是经典if语句的替代。
@appl('child_app_slug') // @if(Auth::check() && Auth::user()->uses('child_app_slug')) // user can use child_app_slug @endappl @allowedappl('child_app_slug', $view) // @if(Auth::check() && Auth::user()->allowed('child_app', $view)) // show child_app specific content // Access to specific content within the child_app @endallowedappl @appl('child_app_1|child_app_2', 'all') // @if(Auth::check() && Auth::user()->can('child_app_1|child_app_2', 'all')) // user can use child_app_1 and also child_app_2 @else // something else @endappl
为了更好地理解,请参阅IsApplUserContract。