首页 文章

Laravel 5.5登录后重定向到特定路由

提问于
浏览
-1

我试图在Laravel 5.5中做这么简单的事情,我无法相信有办法做到这一点 .

我有一个按钮,如果用户通过身份验证,它应该执行某些操作,如果用户未经过身份验证,则应将用户发送到登录页面,并在用户登录后重定向到另一个页面 . 如何将其他页面URL作为参数传递到登录页面?

该按钮的我的刀片代码是:

@if(empty($_user))
<a href="{{ route('login') }}" class="button button-reply icon-button reply-icon" title="Post a reply">Post Reply</a>
@else
<a href="#reply-post" class="button button-reply icon-button reply-icon" title="Post a reply">Post Reply</a>
@endif

我错过了什么吗?有没有办法做这么基本的事情?我知道如果最终页面受到auth中间件的保护,Laravel会重定向,但在这种情况下,页面不在该中间件的后面 .

1 回答

  • 0

    我通过将最终的URL传递给登录页面然后将其作为隐藏输入包含在登录表单中来解决它,最后在LoginController的 redirectPath() 函数中使用它 .

    这是Blade页面中的代码,用于将redirectTo传递给URL中的登录页面:

    <a href="{{ route('login') }}?redirectTo={{ urlencode(route('topic', $topic->id)) }}" class="button button-reply icon-button reply-icon" title="Post a reply">Post Reply</a>
    

    然后在登录表单中:

    @if(!empty(app('request')->input('redirectTo')))
    <input type="hidden" name="redirectTo" value="{{ urldecode(app('request')->input('redirectTo')) }}" />
    @endif
    

    并在 redirectPath() 函数中:

    /**
     * If there's a defined redirectTo, use it
     */
    if (!empty(request()->input('redirectTo'))) {
        $this->redirectTo = request()->input('redirectTo');
    }
    

    这对我来说很好,但我仍然认为Laravel应该有一个内置的解决方案,所以如果有人知道内置的解决方案,或者更好的解决方法,请分享到这里!

相关问题