Laravel trait重定向回会话

问题描述:

//inside of trait method
redirect()->back()->with('success', $response['message'])->send();

I have a method inside of trait required redirect back to previous page with flash session, but about code only redirect back without session. (controller call this trait method)

I also tried Session::flash(); but its not working too

this method will use many times in my app, therefor I need this inside of my trait

  //在trait方法内部
redirect() - > back() - > with(  'success',$ response ['message']) - > send(); 
  code>  pre> 
 
 

我有一个trait内部的方法需要重定向回到上一页 闪存会话,但关于代码只重定向没有会话。 (控制器调用此特征方法) p>

我还尝试了 Session :: flash(); code>但它也不能正常工作 p>

这个方法会在我的应用程序中多次使用,因此我需要在我的特性中使用 p> div>

You have to return & I don't know whether you need to use send or not so use like

return redirect()->back()->with('success', $response['message']);

See how laravel do it https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Exceptions/Handler.php#L158-L160

Just return it from trait in your controller, don't use send() method if you want to redirect back with session.

// In your trait

return redirect()->back();

then in your controller,

$response = mytraitFunction();
if ($response is instanceof RedirectResponse)
return $response->with('success', $response['message']);

Don't forget to use: use Illuminate\Http\RedirectResponse;