拉拉维尔重定向::路由不起作用

Laravel Redirect::route not working

本文关键字:不起作用 路由 重定向      更新时间:2023-09-26

我正在尝试将Redirect::route()与基于SPA ajax的应用程序一起使用,但由于某种原因它不起作用,我的意思是,页面的位置不会更改为应该重定向的位置。我正在尝试在用户登录和注销时使用它。

路线:

Route::get('/', ['as' => 'root','uses' => 'HomeController@index']);
Route::group(['before' => 'ajax'], function() {
    Route::get('/login', ['as' => 'login', 'uses' => 'UserController@login'])->before('guest');
    Route::post('/login', ['as' => 'signin', 'uses' => 'UserController@signin']);
    Route::get('/logout', ['as' => 'logout', 'uses' => 'UserController@logout'])->before('auth');
});

控制器的方法:

public function signin() {
    $user = [
        'email' => Input::get('email'),
        'password' => Input::get('password')
    ];
    if (Auth::attempt($user)) {
        //return Redirect::route('root')->with('flash_notice', 'You are successfully logged in!'); // not redirecting
        // I have to use a json response instead and refresh from the js function, but I am losing the notice
        return Response::json([
            'status' => true,
            'notice' => 'You are successfully logged in!'
        ]);
    }
    return Response::json([
        'status' => false,
        'error' => 'Your email or password combination was incorrect.'
    ]);
}
public function logout() {
    Auth::logout();
    return Redirect::route('root')->with('flash_notice', 'You are successfully logged out.'); // not redirecting
}

正如我评论的那样,我不能使用Redirect::route().相反,在响应成功的 ajax 请求时,我被迫在我的 js 函数中使用 window.location.replace()

/* Login */
$(document).on('click', '#login-submit', function(event) {
    event.preventDefault();
    $.post('/login', $('#login-form').serialize(), function(data) {
        if (data['status'] == true) {
            window.location.replace('/home/main');
        } else {
            $('#error').removeClass('hidden');
            $('#error').html(data['error']);
        }
    });
});

这样做不仅对我来说是不可取的,而且我也丢失了通知消息;它们只是在瞬间出现,然后在页面加载后消失。

为什么会这样?如何使重定向::route()' 工作?或者至少,当通知只是瞬间出现并消失并设法显示通知时,我如何避免这种丑陋的效果?

问题是你不能根据来自ajax请求的标头重定向用户。如果你想让服务器在ajax请求中执行验证,那么你的方法就是完全正确的。要在成功登录后保留通知,您可以这样做

设置超时

您的用户将有足够的时间来阅读通知。

setTimeout(function() {
    window.location.replace('/home/main');
}, 500);

通知旁边的添加继续按钮

他们将有尽可能多的时间,但它可能不是最好的用户体验设计。

$('#results').html('<div class="alert alert-info">You have been successfully logged in. Click here to continue');
$('#results').click(function() {
    window.location.replace('/home/main');
});

重定向用户并通过 GET 请求发送通知

将按摩附加为获取参数,并在脚本启动时检查它。

window.location.replace('/home/main?message="You have been successfully logged in"');

然后

function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​
var message = GetURLParameter('message');
if(message) {
   $('#results').html('<div class="alert alert-info">' + message + '</div>');
}