簡體   English   中英

使用自定義刷新的會話數據注冊后的Laravel自定義重定向

[英]Laravel custom redirect after Registration with custom flashed session data

我正在使用Laravel 5.8構建一個應用程序,在該應用程序中,注冊或登錄后,用戶將被重定向到自定義頁面,並在頁面上顯示閃爍的會話數據,並顯示“歡迎!”。

我注意到RegisterController中的默認重定向行為是一個簡單的字符串,不允許我添加自定義重定向。

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

我嘗試修改此默認行為,將字符串替換為函數:

protected function redirectTo()
{
    /* generate URL dynamically */
     return redirect('/custompage')->with('status', 'Welcome!');
}

但我得到警告

ErrorException(E_WARNING)標頭不能包含多個標頭,檢測到新行

那么,如何重定向到自定義頁面並添加我的自定義刷新數據? 當然無需修改任何供應商代碼。

提前致謝

更改為

protected function redirectTo()
{
    /* generate URL dynamicaly */
     return '/custompage';
}

它僅返回path而不返回,並且您在這里不需要redirect()

並根據需要使用Session::flash()Session::put()添加會話數據。

您可以用不同的方式實現您所描述的內容。 一種簡單的方法是在RegisterController中使用自定義路由的url,然后將該路由添加到您的路由並相應地定義控制器函數。

您將像這樣保持RegisterController:

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

然后為其添加一條路線:

Route::any('custompage', array('as' => 'custompage', 'uses' => 'HomeController@custompage'));

並根據需要定義控制器功能。

您可以在redirectTo方法中做到這一點。 此方法應返回字符串而不是響應對象。 所以應該是這樣

protected function redirectTo()
{
    /* flash data to the session here */
    session(['status', 'Welcome']);
     return '/custompage';
 }

暫無
暫無

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

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