如何将用户重定向到以前的URL在Laravel中注册后

问题描述 投票:0回答:1

使用Laravel 5.6中内置的Auth系统,我的网站上有多个页面,我需要用户注册或登录才能执行购买等操作...

我的登录信息重定向回到用户登录前的上一页,但似乎无法通过注册来实现这一点。

我正在寻找的例子:

  1. 用户点击第X页的注册。
  2. 用户进入注册页面
  3. 然后将用户重定向回第X页而不是主页
  4. 用户始终会重定向回以前所在的页面。

这是我的RegisterController.php:

//protected $redirectTo = '/'; (Originally Returning Back To Home)

//WHAT I'M USING NOW TO TRY AND REDIRECT BACK TO PREVIOUS PAGE

    protected function redirectTo()
    {
    return url()->previous();
    }

// Guards/Redirects user to home if logged in and tries to access register page again.

    public function __construct()
    {
        $this->middleware('guest');
    } 


// If users email exists in my transaction database, I show them a Thank you message upon registering. And Still want to redirect them back to previous page.

    public function registered($request, $user) {
        $where = [
            ['customer_email', $user->email],
            ['user_id', null],
        ];

        if (Transaction::where($where)->exists()) {
            Transaction::where($where)->update(['user_id' => $user->id]);

            return url()->previous()
                ->with('success', 'Thank you for your previous transaction! Go to your Profile to review your transaction history.');
        }
    }

以下是我使用Login Controller的方法,但是没有使用寄存器控制器。

public function showLoginForm()
{
    if (session('link')) {
        $myPath     = session('link');
        $loginPath  = url('/login');
        $previous   = url()->previous();

        if ($previous = $loginPath) {
            session(['link' => $myPath]);
        }else{
            session(['link' => $previous]);
        }
    }
    else{
        session(['link' => url()->previous()]);
    }
    return view('auth.login');
}

protected function authenticated(Request $request, $user)
{
    return redirect(session('link'));      
}

任何帮助,将不胜感激。

php laravel authentication
1个回答
1
投票

好的,所以你可以用类似登录控制器的方式来做。

您首先需要在Register Controller中创建以下功能以覆盖默认值并将URL保存到会话中

/**
* Show the application registration form.
*
* @return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
    if (session('link')) {
        $myPath     = session('link');
        $registerPath  = url('/register');
        $previous   = url()->previous();

        if ($previous = $registerPath) {
            session(['link' => $myPath]);
        }else{
            session(['link' => $previous]);
        }
    } else{
        session(['link' => url()->previous()]);
    }
    return view('auth.register');
}

然后在注册控制器中再次创建以下功能

protected function redirectTo()
{
    return redirect(session('link'))->with('success', 'Thank you for your previous transaction! Go to your Profile to review your transaction history.');
}

您可能还想在其中设置默认路由,只需直接访问寄存器表单即可。所以

protected function redirectTo()
{
    if(session('link')){
        return redirect(session('link'))->with('success', 'Thank you for your previous transaction! Go to your Profile to review your transaction history.');
    }
    return redirect('/home')->with('success', 'Thank you for your previous transaction! Go to your Profile to review your transaction history.');
}
© www.soinside.com 2019 - 2024. All rights reserved.