没有不值得去解决的问题,也没有不值得去学习的技术!

Preserve Query Parameters After 302 Redirects in Laravel 6 Using Terminable Middleware

In Laravel 6, the query parameters of a request are lost after a 302 redirect

1. In Laravel 6, the query parameters of a request are lost after a 302 redirect. See Figure 1.

In Laravel 6, the query parameters of a request are lost after a 302 redirect
Figure 1

2. I originally planned to check whether this query parameter exists during the specific request, and if it does, automatically append it after the 302 redirect. However, this approach cannot prevent other subsequent requests from potentially having similar issues. I ultimately decided to use a post-middleware to uniformly handle the issue of request query parameters being lost after a 302 redirect.

3. I placed the newly created middleware into the web middleware group because it only needs to be applied to the Web UI.

PHP

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            // ...
            \App\Http\Middleware\ThemeEditorQueryParam::class,
        ]
    ];


4. Implement a redirect to a named route in a controller method.

PHP
return redirect()->route('account_login');

5. Add query parameters based on league/uri by running composer require league/uri-components and composer require league/uri. Reference: In PHP 7.4, add query parameters based on league/uri for URIs.

6. The code implementation in the middleware is as follows.

PHP
<?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, and it 302 redirects to: https://xxx.local/account/login . This meets expectations. See Figure 2.

Open the URL: https://xxx.local/products/ri-yuan-ce-shi-bian-zhong?variant=656 , without the oseid parameter, and it 302 redirects to: https://xxx.local/account/login . Meets expectations
Figure 2

8. Open the URL: https://xxx.local/products/ri-yuan-ce-shi-bian-zhong?variant=656&oseid=6462e3db4cd20 , with the oseid parameter, and it 302 redirects to: https://xxx.local/account/login?oseid=6462e3db4cd20 . This meets expectations. See Figure 3.

Open the URL: https://xxx.local/products/ri-yuan-ce-shi-bian-zhong?variant=656&oseid=6462e3db4cd20 , with the oseid parameter, and it 302 redirects to: https://xxx.local/account/login?oseid=6462e3db4cd20 . Meets expectations
Figure 3

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系