Skip to content

Commit

Permalink
Support redirection passing parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Sep 17, 2024
1 parent 0faffdf commit 6b6d725
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/Wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ public function getActionUrl(string $method, $parameters = [])
*
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @param array $parameters
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectToStep($step = null, $setLastIndex = true)
public function redirectToStep($step = null, $setLastIndex = true, array $parameters = [])
{
if (is_null($step)) {
$step = $this->stepRepo()->next();
Expand All @@ -206,7 +207,7 @@ public function redirectToStep($step = null, $setLastIndex = true)
$this->setLastProcessedIndex($step->index());
}

return redirect($this->getActionUrl('create', [$step->slug()]));
return redirect($this->getActionUrl('create', ['step' => $step->slug(), ...$parameters]));
}

/**
Expand Down
31 changes: 21 additions & 10 deletions src/Wizardable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

trait Wizardable
{
Expand All @@ -31,14 +32,21 @@ trait Wizardable
*/
public function create(Request $request, $step = null)
{
// Get the query parameters.
$parameters = $request->query();
unset($parameters['_trigger']);
$resetParam = Arr::pull($parameters, '_reset');

// Reset the wizard cache when passing the `_reset` query parameter.
if ($request->query('_reset') === '1') {
if ($resetParam === '1') {
$this->wizard()->cache()->clear();

// Clean up the wizard event.
$this->cleanUpWizard($request);

return $this->redirectToLastProcessedStep($request, 0);
return $this->redirectToLastProcessedStep(
$request, 0, $parameters
);
}

// Before wizard step create event.
Expand All @@ -51,7 +59,7 @@ public function create(Request $request, $step = null)
// If step is null, redirect to last processed index.
if (is_null($step)) {
return $this->redirectToLastProcessedStep(
$request, $lastProcessedIndex
$request, $lastProcessedIndex, $parameters
);
}

Expand All @@ -61,7 +69,7 @@ public function create(Request $request, $step = null)
if ($step->index() !== $lastProcessedIndex) {
// Redirect to last processed step.
return $this->redirectToLastProcessedStep(
$request, $lastProcessedIndex
$request, $lastProcessedIndex, $parameters
);
}

Expand Down Expand Up @@ -202,13 +210,14 @@ protected function renderDoneView(Request $request)
*
* @param \Illuminate\Http\Request $request
* @param int $lastProcessedIndex
* @param array $parameters
* @return \Illuminate\Http\RedirectResponse
*/
protected function redirectToLastProcessedStep(Request $request, int $lastProcessedIndex)
protected function redirectToLastProcessedStep(Request $request, int $lastProcessedIndex, array $parameters = [])
{
$lastProcessedStep = $this->wizard()->stepRepo()->get($lastProcessedIndex);

return $this->redirectToStep($lastProcessedStep->slug(), false);
return $this->redirectToStep($lastProcessedStep->slug(), false, $parameters);
}

/**
Expand All @@ -227,11 +236,12 @@ protected function canValidate(Request $request)
*
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @param array $parameters
* @return \Illuminate\Http\RedirectResponse
*/
protected function redirectToStep($step = null, $setLastIndex = true)
protected function redirectToStep($step = null, $setLastIndex = true, array $parameters = [])
{
return $this->wizard()->redirectToStep($step, $setLastIndex);
return $this->wizard()->redirectToStep($step, $setLastIndex, $parameters);
}

/**
Expand All @@ -241,11 +251,12 @@ protected function redirectToStep($step = null, $setLastIndex = true)
*
* @param string|\Ycs77\LaravelWizard\Step|null $step
* @param bool $setLastIndex
* @param array $parameters
* @return \Illuminate\Http\RedirectResponse
*/
protected function redirectTo($step = null, $setLastIndex = true)
protected function redirectTo($step = null, $setLastIndex = true, array $parameters = [])
{
return $this->redirectToStep($step, $setLastIndex);
return $this->redirectToStep($step, $setLastIndex, $parameters);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/WizardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testRedirectToOtherStep()
$this->wizard->setStepRepo($stepRepo);

$this->wizard->resolveActionUrlUsing(function (string $method, $parameters = []) {
return url('/wizard/test-wizard/'.$parameters[0]);
return url('/wizard/test-wizard/'.$parameters['step']);
});

// act
Expand Down

0 comments on commit 6b6d725

Please sign in to comment.