dongque3797 2017-02-19 11:03
浏览 238
已采纳

在Laravel成功注册后如何重定向到上一页?

Right now redirectTo is set to /home.

I want to know how I can redirect to the previous page.

I tried using

protected $redirectTo = URL::previous(); 

but I get parse error, expecting ','' or';''

What would be the best solution to approach this problem? I assume I'd need to over-ride the $redirectTo variable somehow with URL::previous() and that would be sufficient.

This is my register controller:

namespace App\Http\Controllers\Auth;

use App\User;
use URL;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        // 'name' => 'required|max:255',
        'username' => 'required|max:255|unique:users',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        // 'name' => $data['name'],
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
}
  • 写回答

2条回答 默认 最新

  • duanruanxian5028 2017-02-19 11:37
    关注

    The RegisterController uses the RegistersUsers trait. When you submit the form, the register() method of this trait is called. It validates the input, creates and authenticates the user and then redirects to the path specified by the redirectPath() method. This method is actually set in the RedirectsUsers trait, which is used by the RegistersUsers trait.

    The redirectPath() method will look for a redirectTo() method on the controller. If doesn't find one, if will redirect to the url specified in the redirectTo property. If it doesn't find this method, it will use whatever is returned from it instead.

    So, if you need to set the redirect path dynamically, put this in your RegisterController:

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

    Read more here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化