簡體   English   中英

如何在創建方法 laravel6 后重定向用戶?

[英]How to redirect a user after a create method laravel6?

我在 front_page 中制作了一個注冊表單,我想在成功注冊后將用戶重定向到 /home 頁面,但是我得到了一個數組並且用戶沒有被重定向。

  {"email":"example@example.com","paypal_email":"example@example.com","updated_at":"2020-02- 
  14 
  08:16:32","created_at":"2020-02-14 08:16:32","id":1}

我的路線:

 Route::post('/', 'Auth\RegisterController@create')->name('auth.register');

注冊控制器:

      public function create(Request $request)
{
    return User::create([
        'email' => $request['email'],
        'paypal_email' => $request['paypal_email'],
        'password' => Hash::make($request['password']),
    ]);


}    

  public $redirectTo = '/home';

改變這個:

public function create(Request $request)
{
    return User::create([
        'email' => $request['email'],
        'paypal_email' => $request['paypal_email'],
        'password' => Hash::make($request['password']),
    ]);


}    

對此:

 public function create(Request $request) { $create = User::create([ 'email' => $request['email'], 'paypal_email' => $request['paypal_email'], 'password' => Hash::make($request['password']), ]); if($create){ return redirect('/home'); } //Return errors here }

要了解有關 Laravel 重定向的更多信息,請單擊此處

出於一些原因,您不應直接調用create方法。

  1. 請求未被驗證。
  2. 如果您想在注冊期間觸發一些不會發生的事件。
  3. 它不會自動登錄用戶。

create方法實際上是為 laravel trait RegistersUsers內部使用的。 你可以保持這樣的控制器,你只需要改變你的路線來register

Route::post('/', 'Auth\RegisterController@register')->name('auth.register');

通過這樣做,應該創建用戶並將其重定向到主頁。

還有一個原因public $redirectTo = '/home'; 此屬性存在,通過直接使用create方法,您將完全忽略該屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM