spatie/laravel-onboard

一个帮助跟踪用户入门步骤的Laravel包

2.6.0 2024-02-14 13:09 UTC

README

Latest Version on Packagist Total Downloads

此包允许您为应用程序的用户设置入门流程。

以下是设置示例

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个步骤对UserTeam类可用

其他类将仅看到普通步骤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

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

有关报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

此包的原始代码来自Onboard,作者是Caleb Porzio,他慷慨地允许我们继续开发。

许可证

麻省理工学院许可证(MIT)。请参阅许可证文件获取更多信息。