简体   繁体   English

Laravel 5.8登录后重定向

[英]Laravel 5.8 Redirecting after login

In my Laravel application there is the default login page aswell as a login form inside a dropdown. 在我的Laravel应用程序中,默认登录页面以及下拉菜单中的登录表单。 As I have it set up now, after a successfull login a user is redirected to the index page, however, If a user uses the login form in the dropdown I want him to stay on the page he is already. 正如我现在设置的那样,成功登录后,用户将被重定向到索引页面,但是,如果用户在下拉列表中使用登录表单,我希望他停留在已经存在的页面上。

Is there a better practice than adding a seperate method for logging in via the dropdown? 有没有比通过下拉菜单添加单独的登录方法更好的做法?

You can use axios or ajax to login and stay on the page. 您可以使用axiosajax登录并停留在页面上。

Example for axios , in your script write something like this axios示例,在您的脚本中编写如下内容

$("body").on("submit", "#login-form", function (e) {
    var action = $(this).attr("action"); // your login url

    e.preventDefault();

    axios.post(action, $(this).serialize())
     .then(function (response) {
        // do something from the response
        // i.e. display error or update navbar to logged in view
     })
     .catch(function (error) {
        // handle error here
     });
});

Example for ajax , in your script write something like this ajax示例,在脚本中编写如下内容

$("#login-form").submit(function(event){
    event.preventDefault(); //prevent default action 
    var post_url = $(this).attr("action"); //get form action url
    var request_method = $(this).attr("method"); //get form GET/POST method
    var form_data = $(this).serialize(); //Encode form elements for submission

    $.ajax({
        url : post_url,
        type: request_method,
        data : form_data,
        beforeSend: function() {
            // show loading or whatever
        },
        success: function(data) {
            // do something from the response
            // i.e. display error or update navbar to logged in view
        },
        error: function(xhr) { // if error occured            
            var status = xhr.statusText;
            var response = JSON.parse(xhr.responseText);

            // handle error here                       
        }
    });
});

In both examples, I assume that you will have a login form with an ID login-form 在两个示例中,我都假定您将拥有一个带有ID login-form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM