spatie / laravel-livewire-onboard
一个用于跟踪用户入门步骤的 Laravel 包
Requires
- php: ^8.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.9.2
- spatie/once: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0|^9.0
- pestphp/pest: ^1.21|^2.0
- pestphp/pest-plugin-laravel: ^1.1|^2.0
- phpstan/extension-installer: ^1.1|^2.0
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
README
此包允许您为应用的用户设置入门流程。
以下是设置示例
use App\User; use Spatie\Onboard\Facades\Onboard; Onboard::addStep('Complete Profile') ->link('/profile') ->cta('Complete') ->completeIf(function (User $model) { return $model->profile->isComplete(); }); Onboard::addStep('Create Your First Post') ->link('/post/create') ->cta('Create Post') ->completeIf(function (User $model) { return $model->posts->count() > 0; });
然后您可以在模板中按需渲染此入门流程
@if (auth()->user()->onboarding()->inProgress()) <div> @foreach (auth()->user()->onboarding()->steps as $step) <span> @if($step->complete()) <i class="fa fa-check-square-o fa-fw"></i> <s>{{ $loop->iteration }}. {{ $step->title }}</s> @else <i class="fa fa-square-o fa-fw"></i> {{ $loop->iteration }}. {{ $step->title }} @endif </span> <a href="{{ $step->link }}" {{ $step->complete() ? 'disabled' : '' }}> {{ $step->cta }} </a> @endforeach </div> @endif
支持我们
我们投入大量资源创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感激您从家乡寄给我们明信片,提及您正在使用我们哪些包。您可以在 我们的联系页面 上找到我们的地址。我们在 我们的虚拟明信片墙 上公布收到的所有明信片。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-onboard
用法
将 Spatie\Onboard\Concerns\GetsOnboarded
特性和 Spatie\Onboard\Concerns\Onboardable
接口添加到应用中的任何模型或类,例如 User
模型
class User extends Model implements \Spatie\Onboard\Concerns\Onboardable { use \Spatie\Onboard\Concerns\GetsOnboarded; ...
示例配置
在您的 App\Providers\AppServiceProvider.php
中配置您的步骤
use App\User; use Spatie\Onboard\Facades\Onboard; class AppServiceProvider extends ServiceProvider { // ... public function boot() { Onboard::addStep('Complete Profile') ->link('/profile') ->cta('Complete') /** * The completeIf will pass the class that you've added the * interface & trait to. You can use Laravel's dependency * injection here to inject anything else as well. */ ->completeIf(function (User $model) { return $model->profile->isComplete(); }); Onboard::addStep('Create Your First Post') ->link('/post/create') ->cta('Create Post') ->completeIf(function (User $model) { return $model->posts->count() > 0; });
传递给 completeIf
回调函数的变量名必须是 $model
。
用法
现在您可以在任何地方访问这些步骤及其状态。以下是一个示例 blade 模板
@if (auth()->user()->onboarding()->inProgress()) <div> @foreach (auth()->user()->onboarding()->steps as $step) <span> @if($step->complete()) <i class="fa fa-check-square-o fa-fw"></i> <s>{{ $loop->iteration }}. {{ $step->title }}</s> @else <i class="fa fa-square-o fa-fw"></i> {{ $loop->iteration }}. {{ $step->title }} @endif </span> <a href="{{ $step->link }}" {{ $step->complete() ? 'disabled' : '' }}> {{ $step->cta }} </a> @endforeach </div> @endif
查看以下所有可用功能
/** @var \Spatie\Onboard\OnboardingManager $onboarding **/ $onboarding = Auth::user()->onboarding(); $onboarding->inProgress(); $onboarding->percentageCompleted(); $onboarding->finished(); $onboarding->steps()->each(function($step) { $step->title; $step->cta; $step->link; $step->complete(); $step->incomplete(); });
根据条件排除步骤
Onboard::addStep('Excluded Step') ->excludeIf(function (User $model) { return $model->isAdmin(); });
限制步骤到特定类
Onboard::addStep('Limited Step', User::class) ->link('/post/create'); // or Onboard::addStep('Limited Step', 'App\Models\User') ->link('/post/create');
当使用限制步骤时,未限制的步骤将对所有类可用。例如
// Defining User steps Onboard::addStep('Limited User Step', User::class) ->link('/post/create'); // Defining Team steps Onboard::addStep('Limited Team Step', Team::class) ->link('/post/create'); // Defining a step that is available to all classes Onboard::addStep('Normal Step') ->link('/post/create');
上述操作将使 1 个步骤对所有类可用,而 2 个步骤将仅对 User
和 Team
类可用
其他
类只会看到 普通步骤
。 User
类将同时看到 普通步骤
和 限制用户步骤
。 Team
类将同时看到 普通步骤
和 限制团队步骤
。
定义自定义属性并访问它们
// Defining the attributes Onboard::addStep('Step w/ custom attributes') ->attributes([ 'name' => 'Waldo', 'shirt_color' => 'Red & White', ]); // Accessing them $step->name; $step->shirt_color;
示例中间件
如果您想确保您的用户在访问您的 Web 应用程序时被重定向到下一个未完成的入门步骤,您可以使用以下中间件作为起点
<?php namespace App\Http\Middleware; use Auth; use Closure; class RedirectToUnfinishedOnboardingStep { public function handle($request, Closure $next) { if (auth()->user()->onboarding()->inProgress()) { return redirect()->to( auth()->user()->onboarding()->nextUnfinishedStep()->link ); } return $next($request); } }
提示:不要将此中间件添加到更新入门步骤状态的路由,您的用户将无法继续前进,因为他们将被重定向回入门步骤。
测试
composer test
更新日志
请参阅 更新日志 了解最近更改的详细信息。
贡献
请参阅 贡献指南 了解详细信息。
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
鸣谢
本包的原始代码来自 Onboard,该代码由 Caleb Porzio 提供,他慷慨地允许我们继续开发。
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。