【问题标题】:Laravel 5 Session Doesn't Work on RedirectLaravel 5 会话不适用于重定向
【发布时间】:2019-05-19 23:05:14
【问题描述】:

我在开发 Laravel 应用程序时遇到问题。当我从函数设置会话并加载视图时,会话将显示:

Session::flash("test","ABC");

return view('layout.customer');

但是当我设置会话并将其重定向到该页面中的 URL 时,会话将不起作用。我目前正在使用以下代码:

Session::flash("test","ABC");

return redirect()->route('customer.details');

【问题讨论】:

    标签: php laravel session laravel-5 laravel-session


    【解决方案1】:

    仅在最后一个请求中刷新的变量。

    当您发送重定向时,重定向本身是发送给访问者的第一个请求。在这个请求中,现有的闪存会话被清除。
    现在对被重定向到的页面启动第二个请求,并且会话闪烁不再存在。


    更新:

    您可以使用with 函数将闪存数据添加到重定向。

    return redirect()->route('customer.details')->with("test","ABC");
    

    更多信息:https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

    【讨论】:

    • 我需要在提交表单后获取成功消息,最好的方法是什么
    • 我也试过了,当我尝试在视图上打印时它也变空了
    • 您可以尝试使用session('test') 代替session()->get('test') 吗?见laravel.com/docs/5.7/session#retrieving-data
    【解决方案2】:

    您可以在重定向时使用return redirect()->route('customer.details')->with("test","ABC");来刷新数据。以下是相关文档:https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data

    【讨论】:

    • 我用那个试过了,它也不起作用我需要设置任何库进行重定向吗?
    • 你能显示你用来在页面上显示数据的代码吗?
    • [code]@if (session()->has('test'))
      {{ (session()->get('test' )) }}
      @endif[code]
    • 改写{{ session()->get('test') }}
    【解决方案3】:

    在customer.details 控制器中添加行使用Session;在页面顶部。

    【讨论】:

    • 当我用来重定向时,它在同一个控制器上。并且会话已经加载。
    • 然后在您的视图刀片中尝试以下代码:@if(Session::has('test'))

      {{ Session::get('test') }}

      @endif
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-28
    • 2019-07-01
    • 2013-08-07
    • 2021-11-29
    • 2012-03-11
    相关资源
    最近更新 更多