generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from hotwired-laravel/turbo-native-routes
Send flash messages via query string on Turbo Native redirects (recede, resume, & refresh)
- Loading branch information
Showing
4 changed files
with
135 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace HotwiredLaravel\TurboLaravel\Http; | ||
|
||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Str; | ||
|
||
class TurboNativeRedirectResponse extends RedirectResponse | ||
{ | ||
/** | ||
* Factory Method that builds a new instance of the TurboNativeRedirectResponse | ||
* with the given action and forwards the query strings from the given fallback | ||
* URL to the Turbo Native redirect ones. | ||
*/ | ||
public static function createFromFallbackUrl(string $action, string $fallbackUrl): self | ||
{ | ||
return (new self(route("turbo_{$action}_historical_location"))) | ||
->withQueryString((new self($fallbackUrl))->getQueryString()); | ||
} | ||
|
||
/** | ||
* Sets the flashed data via query strings when redirecting to Turbo Native routes. | ||
* | ||
* @param string $key | ||
* @param mixed $value | ||
* @return self | ||
*/ | ||
public function with($key, $value = null) | ||
{ | ||
$params = $this->getQueryString(); | ||
|
||
return $this->withoutQueryStrings() | ||
->setTargetUrl($this->getTargetUrl().'?'.http_build_query($params + [$key => urlencode($value)])); | ||
} | ||
|
||
/** | ||
* Sets multiple query strings at the same time. | ||
*/ | ||
protected function withQueryString(array $params): self | ||
{ | ||
foreach ($params as $key => $val) { | ||
$this->with($key, $val); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the query string as an array. | ||
*/ | ||
protected function getQueryString(): array | ||
{ | ||
parse_str(str_contains($this->getTargetUrl(), '?') ? Str::after($this->getTargetUrl(), '?') : '', $query); | ||
|
||
return $query; | ||
} | ||
|
||
/** | ||
* Returns the target URL without the query strings. | ||
*/ | ||
protected function withoutQueryStrings(): self | ||
{ | ||
$fragment = str_contains($this->getTargetUrl(), '#') ? Str::after($this->getTargetUrl(), '#') : ''; | ||
|
||
return $this->withoutFragment() | ||
->setTargetUrl(Str::before($this->getTargetUrl(), '?').($fragment ? "#{$fragment}" : '')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters