在 Laravel 5.5 中注册后重定向到自定义 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47345633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:58:37  来源:igfitidea点击:

Redirect to Custom URL after Registration in Laravel 5.5

laravellaravel-5redirectlaravel-authentication

提问by Sridhar

I am working on cart in laravel 5.5. Whenever guests click on "Add to cart", i am redirecting to login. If they have account, they will login and redirecting to product info they have selected. Otherwise they are registering. I wanted to redirect to customer selected product after registration.For Login, this is working fine.. return redirect()->intended(); For Registration intended url not working..

我正在 Laravel 5.5 中处理购物车。每当客人点击“添加到购物车”时,我都会重定向到登录。如果他们有帐户,他们将登录并重定向到他们选择的产品信息。否则他们正在注册。我想在注册后重定向到客户选择的产品。对于登录,这工作正常.. return redirect()->intended(); 对于注册预期网址不起作用..

回答by Amarjit Singh

In Controllers/Auth/RegisterController change protected $redirectTo = '/';at line 30

在 Controllers/Auth/RegisterControllerprotected $redirectTo = '/';中的第 30 行更改

For dynamic URL replace protected $redirectTo = '/';with

对于动态 URL 替换protected $redirectTo = '/';

protected function redirectTo()
{
    /* generate URL dynamicaly */.
    return '/path'; // return dynamicaly generated URL.
}

you can also use return redirect()->intended(/* default uri to redirect user goes here */);

你也可以使用 return redirect()->intended(/* default uri to redirect user goes here */);

The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. A fallback URI may be given to this method in case the intended destination is not available.

重定向器上的预期方法将用户重定向到他们在被身份验证中间件拦截之前尝试访问的 URL。如果预期目的地不可用,则可以为此方法提供后备 URI。

回答by Ridowan Ahmed

Instead of

代替

protected $redirectTo = '/home';

Your can use this method

你可以使用这个方法

protected function redirectTo()
{
    $userName = Auth::user()->name;
    //use your own route
    return route('user.edit', compact('userName'));
}

回答by john godstime

Below the code that does the login add

在执行登录的代码下方添加

 return \Redirect::intended('/default-url-if-user-did-not-come-from-any-page');

回答by mwa91

Use function registered(Request $request, $user)as follows

使用 function registered(Request $request, $user)如下

protected function registered(Request $request, $user)
    {
        if ($redirect_to_selected_prodcut) {
            return redirect('/order/product');
        }

        return redirect()->intended();
    }