简体   繁体   English

Laravel Redirect :: route不起作用

[英]Laravel Redirect::route not working

I am trying to use Redirect::route() with my SPA ajax based app, but it is not working for some reason, I mean, the location of the page doesn't change to where it's supposed to be redirected. 我正在尝试将Redirect::route()与基于SPA ajax的应用程序一起使用,但是由于某些原因它无法正常工作,我的意思是页面的位置不会更改为应该重定向的位置。 I am trying to use this when a user is logging in and logging out. 我正在尝试在用户登录和注销时使用它。

Routes: 路线:

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');
});

Controller's methods: 控制器的方法:

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
}

As I commented, I can't use Redirect::route() . 正如我评论的那样,我不能使用Redirect::route() Instead I am forced to use window.location.replace() in my js functions when responding to a successfull ajax request: 相反,当响应成功的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']);
        }
    });
});

Doing it this way is not only undesirable for me, but I am also losing the notice messages; 这样做对我来说不仅是不希望的,而且还会丢失通知消息。 they just appear for an instant and then go away after the page is loaded. 它们只是出现片刻,然后在页面加载后消失。

Why is this happening? 为什么会这样呢? How can I make Redirect::route()` work? 如何使Redirect :: route()`工作? Or at least, how can I avoid that ugly effect when the notice just shows up for an instant and the go away, and manage to show the notice? 或至少,当通知仅显示一瞬间并消失并设法显示通知时,如何避免这种丑陋的效果?

The thing is that you can not redirect user based on headers from ajax request. 事实是您不能基于ajax请求中的标头重定向用户。 If you want to have validation performed by server in ajax request then your approach is completely correct. 如果您希望服务器在ajax请求中执行验证,那么您的方法是完全正确的。 To keep the notice after successfully login you can do 要在成功登录后保留通知,您可以

Set timeout 设置超时

Your user will have enough time to read the notice. 您的用户将有足够的时间阅读通知。

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

Add continue button next to the notice 在通知旁边添加继续按钮

They will have as much time as needed but it can be not the best UX design. 他们将拥有所需的时间,但这可能不是最佳的UX设计。

$('#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');
});

Redirect user and send notice by GET request 重定向用户并通过GET请求发送通知

Attach massage as get parameters and at the boot of scripts check for it. 将按摩附加为获取参数,并在脚本启动时进行检查。

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

and then 接着

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>');
}

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

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