lenderspender/laravel-wizard
Composer 安装命令:
composer require lenderspender/laravel-wizard
包简介
Control web steps using a wizard.
README 文档
README
Laravel wizard is an easy way to create a clear way of conditional steps.
Installation
You can install the package via composer:
composer require lenderspender/laravel-wizard
Usage
Creating steps
A step is where the logic and view for that particular step is defined. Steps can also be conditional.
You are able to inject any dependencies you need into the step's view and store methods. The Laravel service container will automatically inject all dependencies that are type-hinted.
<?php declare(strict_types=1); namespace App\Http\Controllers; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Http\Request; use LenderSpender\LaravelWizard\StepDetails; use LenderSpender\LaravelWizard\WizardStep; use Symfony\Component\HttpFoundation\Response; class FirstStep extends WizardStep { public function view(): Response { return view('steps.first-step'); } public function store(Request $request): void { $data = $request->validate([ 'foo' => 'required', ]); SomeModel::update(['foo' => $data['foo']]); } public function getStepDetails(): StepDetails { return new StepDetails('First step', 'This is the first step', 'first-step'); } public function isCompleted(?Authenticatable $user): bool { return true; } public function isRequired(?Authenticatable $user): bool { return true; } }
Throwing additional errors
Sometimes you wish to throw additional errors. When the StoreStepException is thrown from the store method in your step.
Users are automatically redirected back to the previous page with errors.
use LenderSpender\LaravelWizard\Exceptions\StoreStepException; public function store(Authenticatable $user) { if (! $user->emailVerified()) { throw new StoreStepException('Email address is not yet verified'); } $user->update(['foo' => 'bar']); }
Setup controller and routes
Create a new Controller that will handle your steps.
<?php declare(strict_types=1); namespace App\Http\Controllers; use Illuminate\Contracts\Auth\Authenticatable; use LenderSpender\LaravelWizard\Wizard; use Symfony\Component\HttpFoundation\Response; class WizardController { public function show(string $step, Authenticatable $user): Response { return $this->getWizard($user) ->view($step); } public function store(string $step, Authenticatable $user): Response { $wizard = $this->getWizard($user); if ($redirect = $wizard->store($step)) { return $redirect; } return redirect(action( [WizardController::class, 'show'], [ 'step' => (string) $wizard->nextStep($wizard->getStepFromSlug($step)), ] )); } private function getWizard(Authenticatable $user): Wizard { return new Wizard( [ new FirstStep(), new SecondStep(), ], false, $user ); } }
Then you should define your routes:
Route::get('/wizard/{step}', [WizardController::class, 'show']); Route::post('/wizard/{step}', [WizardController::class, 'store']);
Setup wizard
A new wizard can be initialized by creating a new Wizard object.
$wizard = new Wizard([ FirstStep::class, new FirstStep($param), [FirstStep::class => ['param' => $param]] ]);
lenderspender/laravel-wizard 适用场景与选型建议
lenderspender/laravel-wizard 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.51k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 lenderspender/laravel-wizard 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lenderspender/laravel-wizard 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-06-19