In Laravel 6, the problem of missing request query parameters after the 302 jump is uniformly processed based on the post middleware
1. In Laravel 6, the request with query parameters is lost after the 302 jump. as shown in Figure 1
2. The original plan to determine whether the query parameter exists at a specific request. If it exists, it will be automatically brought with it after the 302 jump. However, this scheme cannot be avoided that there may be other requests and similar problems in the follow-up. In the final decision, the problem of missing request query parameters after the 302 jump is uniformly handled based on the post-middleware.
3. Put the newly created middleware into the web middleware group, because it only needs to be applied to the web UI.
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
// ...
\App\Http\Middleware\ThemeEditorQueryParam::class,
]
];
4. Implement redirect to named route in a controller method
return redirect()->route('account_login');
5. Add query parameters based on the League/URI, and execute the composer require League/uri-components and composer require League/URI. Reference: In PHP 7.4, for URI, add query parameters based on League/URI
6. The code in the middleware is implemented as follows
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\RedirectResponse;
use League\Uri\Uri;
use League\Uri\UriModifier;
class ThemeEditorQueryParam
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
// 当响应为重定向时,判断请求中是否存在请求参数:oseid
if ($response instanceof RedirectResponse) {
$oseid = $request->query('oseid'); // 主题编辑器的主题的预览(2.0)
if (isset($oseid)) {
$uri = Uri::createFromString($response->getTargetUrl());
$editorUri = UriModifier::mergeQuery($uri, 'oseid=' . $oseid);
$response->setTargetUrl($editorUri->jsonSerialize());
}
}
return $response;
}
}
7. Open the URL:https://xxx.local/products/ri-yuan-ce-shi-bian-zhong?variant=656, without the OSEID parameter, 302 jumps to:https://xxx.local/account/login. in line with expectations. as shown in Figure 2
8. Open the URL:https://xxx.local/products/ri-yuan-ce-shi-bian-zhong?variant=656&oseid=6462e3db4cd20, with the OSEID parameter, 302 jumps to:https://xxx.local/account/login?oseid=6462e3db4cd20. in line with expectations. as shown in Figure 3


