PHP code example of sjwatts119 / livewire-wizard

1. Go to this page and download the library: Download sjwatts119/livewire-wizard 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/ */

    

sjwatts119 / livewire-wizard example snippets




namespace App\Livewire;

use Illuminate\View\View;
use SamWatts\LivewireWizard\Livewire\Wizard;
use SamWatts\LivewireWizard\Wizard\WizardStep;

class YourWizard extends Wizard
{
    public function wizardSteps(): array
    {
        return [
            WizardStep::make(
                title: 'Step 1',
                view: view('step-1'),
            ),
        ];
    }

    public function render(): View
    {
        return $this
            ->currentStep()
            ->view();
    }
}



namespace App\Livewire;

use Illuminate\View\View;
use SamWatts\LivewireWizard\Livewire\Wizard;
use SamWatts\LivewireWizard\Wizard\WizardStep;

class YourWizard extends Wizard
{
    public function wizardSteps(): array
    {
        return [
            WizardStep::make(
                title: 'Your Message',
                view: view('livewire.wizard.message'),
            ),
            WizardStep::make(
                title: 'Your Details',
                view: view('livewire.wizard.details'),
            ),
        ];
    }

    public function render(): View
    {
        return $this
            ->currentStep()
            ->view();
    }
}



namespace App\Livewire;

use Illuminate\View\View;
use SamWatts\LivewireWizard\Livewire\Wizard;
use SamWatts\LivewireWizard\Wizard\WizardStep;

class YourWizard extends Wizard
{
    #[Validate('       WizardStep::make(
                title: 'Confirm',
                view: view('livewire.wizard.confirm'),
                canNavigate: fn () => $this->validatePropertiesForStep(['name', 'message']),
            ),
        ];
    }

    public function render(): View
    {
        return $this
            ->currentStep()
            ->authorise() // Validate the 'name' and 'message' properties if we are on step 'Confirm'.
            ->view();
    }
}

$this->currentStep()->view();

$this->currentStep()
    ->authorise()
    ->view();

$this->currentStep()
    ->authorise(aborts: false)
    ->view();

namespace App\Livewire;

use Illuminate\View\View;
use SamWatts\LivewireWizard\Livewire\Wizard;
use SamWatts\LivewireWizard\Wizard\WizardStep;
use SamWatts\LivewireWizard\Exceptions\StepNotAuthorisedException;

class YourWizard extends Wizard
{
    /*
     * Gracefully handle the exception.
     */
    public function exception($e, $stopPropagation) {
        if ($e instanceof StepNotAuthorisedException) {
            $this->notify('Post is not found');
            $stopPropagation();
        }
    }

    public function wizardSteps(): array
    {
        return [
            // Steps with canNavigate closures...
        ];
    }

    public function render(): View
    {
        return $this
            ->currentStep()
            ->authorise(aborts: false) // Throws StepNotAuthorisedException if rules fail...
            ->view();
    }
}

$this->steps(); // Returns a Collection of all WizardStep instances, keyed by their titles.

$this->currentStep(); // Returns the current WizardStep instance.

$this->nextStep(); // Returns the next WizardStep instance, or null if one does not exist.

$this->previousStep(); // Returns the previous WizardStep instance, or null if one does not exist.

$this->firstStep(); // Returns the first WizardStep instance.

$this->lastStep(); // Returns the last WizardStep instance.

$this->step('step-title'); // Returns the WizardStep instance with the given title, or null if it does not exist.

$this->navigateToPreviousStep(); // Sets the current step to the previous step, or does nothing if one does not exist.

$this->navigateToNextStep(); // Sets the current step to the next step, or does nothing if one does not exist.

$this->navigateToStep('step-title'); // Sets the current step to the WizardStep instance with the given title, or does nothing if it does not exist.

$step->title(); // Returns the title of the step.

$step->view(); // Returns the view of the step.

$step->is($otherStep); // Returns a boolean value indicating whether the provided WizardStep has the same title as this step.

$step->authorise(); // Executes the canNavigate closure and returns the step instance.

$step->canNavigate(); // Executes the canNavigate closure and returns a boolean value.
bash
php artisan make:wizard