PHP code example of lexal / laravel-stepped-form

1. Go to this page and download the library: Download lexal/laravel-stepped-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lexal / laravel-stepped-form example snippets


$app->register(Lexal\LaravelSteppedForm\ServiceProvider\ServiceProvider::class);

    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();
    

     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,
            ],
        ],
 
        // ...
     ];
     

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

   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'));
       }
   }
   
shell
php artisan vendor:publish --provider="Lexal\LaravelSteppedForm\ServiceProvider\ServiceProvider"