【问题标题】:Laravel Redirect Intended does nothingLaravel Redirect Intended 什么都不做
【发布时间】:2018-05-16 14:01:39
【问题描述】:

我目前有一个从 Vue 组件登录时点击的身份验证功能。现在它使用户登录,但控制器没有发生重定向。我不确定是否使用 Vue 组件会导致这种情况。如果是这样,也许我可以在响应中返回预期的 URL?

public function authenticate(Request $request)
    {

        //Validate the login and log errors if any
        $this->validate($request, [
            'email'     => 'required',
            'password'  => 'required',
        ]);

        //if they have stuff posted get it
        $email      = $request->get('email');
        $password   = $request->get('password');


        //See if they are actually a user
        if (Auth::attempt(['email' => $email, 'password' => $password])) {

          return redirect()->intended('/dashboard');

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }
    }

我的 login.vue 组件中的登录方法:

login(){

             this.isLoading = true;

             this.form.post('/login')
                .then(data => {

                  this.isLoading = false

                  if(data.response == 'success'){

                     //Maybe get a url in the response and redirect here??

                  } else {
                    this.serverError= data.error
                  }

                })
                .catch(error => {
                  this.isLoading = false
                })

           }

使用 Laravel 5.4

【问题讨论】:

  • 从 vuejs 重定向。不是来自 laravel
  • 在我看来,从 Laravel 重定向 RESTful API 并没有多大意义。我会返回用户实例或类似的东西并从前端进行重定向。
  • 我刚刚发布了对我有用的答案。我只是将预期的 URL 返回到我的前端并让它处理它。谢谢@user2486
  • 不要忘记选择您自己的答案作为已接受。
  • 您没有使用开箱即用的身份验证设置的任何原因,默认情况下会为您完成这一切吗?

标签: php laravel vue.js


【解决方案1】:

对于任何人来说:

在我的身份验证功能中:

if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return response()->json([
              'response' => 'success',
              'url' => Session::get('url.intended', url('/'))
            ]);

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }

在我的vue组件登录方法中

if(data.response == 'success'){
                    //console.log(data);
                    window.location.href = data.url
                  } else {
                    this.serverError= data.error
                  }

【讨论】:

  • 检查我的回答。似乎您希望得到 URL 字符串结果而不是 HTML 响应。因为 vue 是一个 javascript
  • @KennethSunday 是的,看起来我们发布了相同类型的东西。起初我只是感到困惑,因为没有发生重定向,但我意识到我需要我的前端来处理我的设置
【解决方案2】:

为什么不使用预期的方法,而不是使用redirect()->route()

或者您正在等待 URL 响应。你不应该使用redirect() 方法。

对于您给定的代码,您可能需要考虑这一点。

if (Auth::attempt(['email' => $email, 'password' => $password])) {

            return response()->json([
              'response' => 'success',
              'url' => Session::get('url.intended', route('route_of_your_dashboard'))
            ]);

        } else {
          return response()->json([
            'response' => 'error',
            'error' => 'Email or Password not correct.',
          ]);
        }

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2023-03-07
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2018-12-17
    相关资源
    最近更新 更多