简体   繁体   English

使用自定义刷新的会话数据注册后的Laravel自定义重定向

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

I am building an app with Laravel 5.8 where, after registration or login, the user is redirected to a custom page along with a flashed session data, displayed on the page, that says "Welcome!". 我正在使用Laravel 5.8构建一个应用程序,在该应用程序中,注册或登录后,用户将被重定向到自定义页面,并在页面上显示闪烁的会话数据,并显示“欢迎!”。

I noticed that default redirect behavior in the RegisterController is a simple string, that does not allow me to add my custom redirect. 我注意到RegisterController中的默认重定向行为是一个简单的字符串,不允许我添加自定义重定向。

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

I tried modifying this default behavior replacing the string with a function: 我尝试修改此默认行为,将字符串替换为函数:

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

But I get the Warning 但我得到警告

ErrorException (E_WARNING) Header may not contain more than a single header, new line detected ErrorException(E_WARNING)标头不能包含多个标头,检测到新行

So, how to redirect to the custom page AND add my custom flashed data? 那么,如何重定向到自定义页面并添加我的自定义刷新数据? Of course without modifying any vendor code. 当然无需修改任何供应商代码。

Thanks in advance 提前致谢

change this to 更改为

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

It only returns path not the and you do not need redirect() here. 它仅返回path而不返回,并且您在这里不需要redirect()

and added session data using Session::flash() or Session::put() depending on your requirement. 并根据需要使用Session::flash()Session::put()添加会话数据。

You can achieve what you described in different ways. 您可以用不同的方式实现您所描述的内容。 A simple way will be to use the url of your custom route in the RegisterController, then add that route to your route and define a controller function accordingly. 一种简单的方法是在RegisterController中使用自定义路由的url,然后将该路由添加到您的路由并相应地定义控制器函数。

You will keep your RegisterController like this: 您将像这样保持RegisterController:

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

Then add a route for it: 然后为其添加一条路线:

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

And Define a controller function as you desire. 并根据需要定义控制器功能。

You can do that in the redirectTo method. 您可以在redirectTo方法中做到这一点。 This method should return a string not a response object. 此方法应返回字符串而不是响应对象。 So it should be like this 所以应该是这样

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