dongxian7471 2018-11-06 10:32 采纳率: 100%
浏览 67
已采纳

Laravel App执行两个请求,但第二个不需要

I've found out, that my Laravel (5.7) App has a strange behavior in some case where it performs two requests of form submits. Last week everything worked fine.

E.g: There is a registration form. When a user wants to register and submits the form, he fails validation, because the entered email-address already exists (which it doesn't right before he submitted the form). A glance in my database shows me, that the user was created and logged in. So it seems, that the form somehow performs two requests.

This behavior happens on several forms all over the application but not on all. This causes some very bad actions (the one above, emails are sent double, images are uploaded twice etc etc).

One possible problem-causer could be a new Middleware, where I access the current request and perform some actions on it:

class CookiebannerMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        $cookie_name = config('cookiebanner.cookie_key');
        $cookie_value = config('cookiebanner.cookie_value');
        $cookie_lifetime = config('cookiebanner.cookie_lifetime');

        $response = $next($request);

        if(!$request->hasCookie($cookie_name)){
            $response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
            return $response;
        }

        return $next($request);
    }
}

Or are there any other possibilities which could cause such problems?


Update

The problem was my CookiebannerMiddleware. The problem is fixed with the following solution:

class CookiebannerMiddleware
{
    public function handle(Request $request, Closure $next)
    {

        $cookie_name = config('cookiebanner.cookie_key');
        $cookie_value = config('cookiebanner.cookie_value');
        $cookie_lifetime = config('cookiebanner.cookie_lifetime');

        if(!$request->hasCookie($cookie_name)){
            return $next($request)->withCookie($cookie_name, $cookie_value, $cookie_lifetime);
        }

        return $next($request);
    }
}
  • 写回答

3条回答 默认 最新

  • duangutian1426 2018-11-06 10:53
    关注

    Avoid calling $next($request) twice:

    class CookiebannerMiddleware
    {
        public function handle(Request $request, Closure $next)
        {
            $cookie_name = config('cookiebanner.cookie_key');
            $cookie_value = config('cookiebanner.cookie_value');
            $cookie_lifetime = config('cookiebanner.cookie_lifetime');
    
            $response = $next($request);
    
            if(!$request->hasCookie($cookie_name)){
                $response->cookie($cookie_name, $cookie_value, $cookie_lifetime);
            }
    
            return $response;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘