在 Laravel 6 中 302 跳转至当前页面的实现,且剥离掉请求参数

1、现阶段的需求是打开网址:https://wshop.local/?theme_preview_code=eyJpdiI6InBLRHBMXC9NSmo1Y2hkTFZUeEZLUEx3PT0iLCJ2YWx1ZSI6IlBlN3lFdnJiWmtNVVBjeHY5aENjMW5uMFhEVXVjNXBZeGNDK3dsaFFDRTFwQnBsTFFUdnpTNnpoSWxcL2x4SUFLRzVFUTh3bTUzSmNJb1NLOXFKd2puWVZRNExVdUk1QzlYSG9vUkFlZFRuSlR6KzZqVmlkSkpBQ3FrejVKRGhmUiIsIm1hYyI6Ijk1YzhlYTgzYmM2MmM3ZmI2Mzk1Yjc2NGZjZTgxZjhjOWZjMDVkNGVjNjAxNTNmNjU5NjdkMjk3ZGE1YjI0N2YifQ== 。在中间件中执行了 Session 相关的操作后,302 跳转至 https://wshop.local/ 。不能够直接跳转至首页,因为需要支持其他页面的跳转。

2、代码实现如下

    /**
     * Handle an incoming request.
     *
     * @param  /Illuminate/Http/Request  $request
     * @param  /Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        /* 主题预览,判断参数:theme_preview_code 是否存在,并且不为空 */
        if ($request->filled('theme_preview_code')) {
            ...

            // 重定向至当前路由,剥离请求参数
            $route = Route::current();
            return redirect()->route($route->uri);
        }


        return $next($request);
    }

3、请求网址: https://wshop.local/cart?theme_preview_code=eyJpdiI6InBLRHBMXC9NSmo1Y2hkTFZUeEZLUEx3PT0iLCJ2YWx1ZSI6IlBlN3lFdnJiWmtNVVBjeHY5aENjMW5uMFhEVXVjNXBZeGNDK3dsaFFDRTFwQnBsTFFUdnpTNnpoSWxcL2x4SUFLRzVFUTh3bTUzSmNJb1NLOXFKd2puWVZRNExVdUk1QzlYSG9vUkFlZFRuSlR6KzZqVmlkSkpBQ3FrejVKRGhmUiIsIm1hYyI6Ijk1YzhlYTgzYmM2MmM3ZmI2Mzk1Yjc2NGZjZTgxZjhjOWZjMDVkNGVjNjAxNTNmNjU5NjdkMjk3ZGE1YjI0N2YifQ== 。302 跳转至 Location: https://wshop.local/cart 。符合预期。如图1

302 跳转至 Location: https://wshop.local/cart 。符合预期

图1

4、请求网址: https://wshop.local/?theme_preview_code=eyJpdiI6InBLRHBMXC9NSmo1Y2hkTFZUeEZLUEx3PT0iLCJ2YWx1ZSI6IlBlN3lFdnJiWmtNVVBjeHY5aENjMW5uMFhEVXVjNXBZeGNDK3dsaFFDRTFwQnBsTFFUdnpTNnpoSWxcL2x4SUFLRzVFUTh3bTUzSmNJb1NLOXFKd2puWVZRNExVdUk1QzlYSG9vUkFlZFRuSlR6KzZqVmlkSkpBQ3FrejVKRGhmUiIsIm1hYyI6Ijk1YzhlYTgzYmM2MmM3ZmI2Mzk1Yjc2NGZjZTgxZjhjOWZjMDVkNGVjNjAxNTNmNjU5NjdkMjk3ZGE1YjI0N2YifQ== 。状态代码: 500 Internal Server Error。Symfony/Component/Routing/Exception/RouteNotFoundException: Route [/] not defined. 如图2

状态代码: 500 Internal Server Error。Symfony/Component/Routing/Exception/RouteNotFoundException: Route [/] not defined.

图2

5、查看 /routes/web.php

<?php
Route::get('/', 'IndexController@index')->name('commoditylist')->middleware('page-cache');

6、调整中间件中的代码实现

    /**
     * Handle an incoming request.
     *
     * @param  /Illuminate/Http/Request  $request
     * @param  /Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        /* 主题预览,判断参数:theme_preview_code 是否存在,并且不为空 */
        if ($request->filled('theme_preview_code')) {
            ...

            // 重定向至当前路由,剥离请求参数
            return redirect()->route(Route::currentRouteName());
        }


        return $next($request);
    }

7、请求网址: https://wshop.local/?theme_preview_code=eyJpdiI6InBLRHBMXC9NSmo1Y2hkTFZUeEZLUEx3PT0iLCJ2YWx1ZSI6IlBlN3lFdnJiWmtNVVBjeHY5aENjMW5uMFhEVXVjNXBZeGNDK3dsaFFDRTFwQnBsTFFUdnpTNnpoSWxcL2x4SUFLRzVFUTh3bTUzSmNJb1NLOXFKd2puWVZRNExVdUk1QzlYSG9vUkFlZFRuSlR6KzZqVmlkSkpBQ3FrejVKRGhmUiIsIm1hYyI6Ijk1YzhlYTgzYmM2MmM3ZmI2Mzk1Yjc2NGZjZTgxZjhjOWZjMDVkNGVjNjAxNTNmNjU5NjdkMjk3ZGE1YjI0N2YifQ== 。已经能够 302 自动跳转至 https://wshop.local 。符合预期。如图3

已经能够 302 自动跳转至 https://wshop.local 。符合预期

图3

8、请求网址: https://wshop.local/cart?theme_preview_code=eyJpdiI6InBLRHBMXC9NSmo1Y2hkTFZUeEZLUEx3PT0iLCJ2YWx1ZSI6IlBlN3lFdnJiWmtNVVBjeHY5aENjMW5uMFhEVXVjNXBZeGNDK3dsaFFDRTFwQnBsTFFUdnpTNnpoSWxcL2x4SUFLRzVFUTh3bTUzSmNJb1NLOXFKd2puWVZRNExVdUk1QzlYSG9vUkFlZFRuSlR6KzZqVmlkSkpBQ3FrejVKRGhmUiIsIm1hYyI6Ijk1YzhlYTgzYmM2MmM3ZmI2Mzk1Yjc2NGZjZTgxZjhjOWZjMDVkNGVjNjAxNTNmNjU5NjdkMjk3ZGE1YjI0N2YifQ== 。302 跳转至 Location: https://wshop.local/cart 。符合预期。如图4

302 跳转至 Location: https://wshop.local/cart 。符合预期

图4

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250760.html

(0)
上一篇 2022年5月1日
下一篇 2022年5月1日

相关推荐

发表回复

登录后才能评论