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

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

图片转代码服务由CSDN问答提供 功能建议

现在redirectTo设置为/home .

nn

我想知道 如何重定向到上一页。

我尝试使用

  protected $ redirectTo = URL :: previous();  
   
 
 

但我得到解析错误,期待',''或';''

解决这个问题的最佳解决方案是什么? 我假设我需要用URL :: previous()以某种方式覆盖$ redirectTo变量,这就足够了。

这是我的寄存器控制器: \ n

 命名空间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扩展Controller 
 {
 
use RegistersUsers; 
 
 / ** 
 *重定向的位置 注册后的用户。
 * 
 * @var string 
 * / 
protected $ redirectTo ='/ home'; 
 
 / ** 
 *创建一个新的控制器实例。
 * 
 * @  return void 
 * / 
public function __construct()
 {
 $ this-> middleware('guest'); 
} 
 
 / ** 
 *获取传入注册请求的验证器 。
 * 
 * @param数组$ data 
 * @return \ Illuminate \ Contracts \ Validation \ Validator 
 * / 
protected function validator(array $ data)
 {
 return Validator :: make($ 数据,[
 //'name'=>'required | max:255',
  'username'=>  'required | max:255 | unique:users',
'email'=>  'required | email | max:255 | unique:users',
'password'=>  'required | min:6 |确认',
]); 
} 
 
 / ** 
 *在有效注册后创建一个新的用户实例。
 * 
 * @param array $ data \  n * @return User 
 * / 
保护函数create(array $ data)
 {
 return User :: create([
 //'name'=> $ data ['name'],
  'username'=> $ data ['username'],
'email'=> $ data ['email'],
'password'=> bcrypt($ data ['password']),\  n]); 
} 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanruanxian5028
    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.

    点赞 评论
  • douxiongzhen2126
    douxiongzhen2126 2018-11-16 10:22

    add this to RegisterController:

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

    Note: if present the field $redirectTo , remove it

    点赞 评论

相关推荐