doukenqiong0588 2016-10-26 16:20
浏览 115
已采纳

Laravel 5.3修改LoginController响应

I want to modify response messages in Laravel 5.3 LoginController. As of now, when I try to login, it redirects me to login page with messages displayed at the top.

But instead of it, I want to use ajax notifications to display messages. How can I modify the logic for response in LoginController?

Sample code for ajax call I am currently using

$.ajax({
  data: data,
  url: 'http://mywebsite.com/login',
  success:function(response){
        console.log(response);
  }
});

P.S - I am a newbie to Laravel and this is my first app using the framework

  • 写回答

2条回答 默认 最新

  • dongpan1365 2016-10-27 07:07
    关注

    One way would be to override sendLoginResponse() and sendFailedLoginResponse() methods from the AuthenticatesUsers trait so that you can check if the request is an ajax (or wants json).

    In you Auth/LoginController add:

    /**
     * Send the response after the user was authenticated.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();
    
        $this->clearLoginAttempts($request);
    
        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                'user' => $this->guard()->user(),
            ]);
        }
    
        return $this->authenticated($request, $this->guard()->user())
            ?: redirect()->intended($this->redirectPath());
    }
    
    /**
     * Get the failed login response instance.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    protected function sendFailedLoginResponse(Request $request)
    {
        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                $this->username() => Lang::get('auth.failed'),
            ], 422);
        }
    
        return redirect()->back()
            ->withInput($request->only($this->username(), 'remember'))
            ->withErrors([
                $this->username() => Lang::get('auth.failed'),
            ]);
    }
    

    This should allow you to keep to same routes for your project.

    Also, if you add dataType: 'json' it should parse the response for you i.e.:

    $.ajax({
      data: data,
      url: 'http://mywebsite.com/login',
      dataType: 'json',
      method: 'post',
      success:function(response){
    
            console.log('success', response);
      },
      error: function () {
          console.log('error', response)
      }
    });
    

    Hope this help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端