lexal/laravel-stepped-form

Laravel & Lumen 的分步表单。

v2.0.0 2023-12-30 16:47 UTC

This package is auto-updated.

Last update: 2024-09-30 01:19:10 UTC


README

PHPUnit, PHPCS, PHPStan Tests

该软件包基于 HTTP Stepped Form 构建,专为 Laravel & Lumen 框架设计。

目录

  1. 需求
  2. 安装
  3. 配置
  4. 使用
  5. 许可证

需求

PHP >=8.1

Laravel ^9.0 || ^10.0

安装

通过 Composer

composer require lexal/laravel-stepped-form

Lumen 框架的额外更改

将以下片段添加到 bootstrap/app.php 文件中的 providers 部分,如下所示

$app->register(Lexal\LaravelSteppedForm\ServiceProvider\ServiceProvider::class);
(回到顶部)

配置

发布配置文件

运行以下命令以发布软件包配置文件

php artisan vendor:publish --provider="Lexal\LaravelSteppedForm\ServiceProvider\ServiceProvider"

可用的配置选项

配置文件 config/stepped-form.php 包含以下选项

  1. renderer - 包含将步骤模板定义转换为响应的渲染器类、实例或服务别名。必须实现 RendererInterface;
  2. redirector - 包含将用户在不同步骤之间进行重定向的重定向器类、实例或服务别名。必须实现 RedirectorInterface;
  3. entity_copy - 包含将给定步骤的实体进行克隆的实体复制类、实例或服务别名。必须实现 EntityCopyInterface;
  4. event_dispatcher - 包含将表单事件分发的事件调度器类、实例或服务别名。必须实现 EventDispatcherInterface;
  5. exception_normalizers - 包含表单将用于将 SteppedFormException 规范化为 Response 实例的异常规范器。更多关于它们的信息,请参阅 HTTP Stepped Form 文档;
  6. forms - 包含所有应用程序表单定义的数组。表单定义必须具有动态表单的构建器类或静态表单的步骤数组和设置类以及表单将存储数据的存储位置。
(回到顶部)

使用

  1. 发布配置文件.

  2. 如有必要,替换为自定义的重定向器、渲染器和实体复制实现。如有必要,添加自定义异常规范器。

  3. 声明您的表单设置。

     use Lexal\HttpSteppedForm\Settings\FormSettingsInterface;                                                            
     use Lexal\SteppedForm\Step\StepKey;
     
     final class FormSettings implements FormSettingsInterface
     {
         public function getStepUrl(StepKey $key): string
         {
             // return step URL
         }
    
         public function getUrlBeforeStart(): string
         {
             // returns a URL to redirect to when there is no previously renderable step
         }
    
         public function getUrlAfterFinish(): string
         {
             // return a URL to redirect to when the form was finishing
         }
     }
    
     $formSettings = new FormSettings();
  4. 添加表单定义。

    • 静态表单
      return [
         // ...
      
         'forms' => [
             'customer' => [
                 'steps' => [
                     'customer' => CustomerStep::class,
                     'broker' => BrokerStep::class,
                     'confirmation' => ConfirmationStep::class,
                     // other steps
                 ],
                 'settings_class' => FormSettings::class,
                 'storage' => SessionStorage::class,
                 'session_storage' => SessionSessionKeyStorage::class,
             ],
         ],
      
         // ...
      ];
    • 动态表单
      return [
         // ...
      
         'forms' => [
             'customer' => [
                 'builder_class' => CustomBuilder::class,
                 'settings_class' => FormSettings::class,
                 'storage' => SessionStorage::class,
                 'session_storage' => SessionSessionKeyStorage::class,
             ],
         ],
      
         // ...
      ];
  5. 在控制器中使用 Stepped Form。Stepped Form 将具有 "stepped-form." + 表单键表单配置的服务别名。

    ServiceProvider.php

    use Lexal\HttpSteppedForm\SteppedFormInterface;
    
    $this->app->when(CustomerController::class)
         ->needs(SteppedFormInterface::class)
         ->give('stepped-form.customer');

    CustomerController.php

    use Lexal\HttpSteppedForm\SteppedFormInterface;
    
    final class CustomerController
    {
        public function __construct(private readonly SteppedFormInterface $form)
        {
        }
    
        // POST /customers
        public function start(): Response
        {
            return $this->form->start(new Customer(), /* nothing or customer id to split different sessions */);
        }
    
        // GET /customers/step/{step-key}
        public function render(string $key): Response
        {
            return $this->form->render($key);
        }
    
        // POST /customers/step/{step-key}
        public function handle(Request $request, string $key): Response
        {
            return $this->form->handle($key, $request);
        }
    
        // POST /customers/cansel
        public function cancel(): Response
        {
            return $this->form->cancel(route('customer.index'));
        }
    }

有关更多信息,请参阅配置文件。

(回到顶部)

许可证

Laravel & Lumen 分步表单采用 MIT 许可证。有关完整的许可证文本,请参阅 LICENSE