du5591 2018-08-03 14:11
浏览 44

Laravel会话数据没有老化

I'm not entirely sure the title is correct as I could not yet figure out how I got to this problem.

What I have:

  • simple model with 2 fields

  • crud controller

  • on create / edit I process the data and redirect with some info. info is passed by one of 2 methods:

    • session()->flash("key","info")
    • redirect()->route("route.name.here")->with("key","info")

I added (to the bottom of my layouts blade file) a line to see what is stored in session hoping I'd get a clue on what's going wrong

@dd(session()->all(),session("_flash"),session("key"))

What is my actual problem? I get the same info again and again.

  • create new instance and flash confirmation to user
  • valdiation returns with error message

It does not matter which one happens I get the returned info again on each successive page call. If I do something to get a new message that message replaces the old one and stays with me on every page of my site. (even the home-page that does nothing but give you some "lorem ipsum" text) If the last info was an error due to failed validation I also keep the old inputs. The index-page only loads the instances and loads the view file (with the data). For example switching through the pages of the index file (I am using laravels pagination as found in the docs) and due to the dump output I can see that nothing in "session" changes (that includes the entry with key "_token". I thought this one should change but I currently can't check with a working site). The form to create a new instance (of the model) is included in the index-page (Bootstrap card header with inline form as it only requries 1 field).

What did I already try?

  • clear the cache
  • delete session files (the get created again)
  • restarting apache
  • rebooting the server (similar to raspberry pi)

I could not figure out how I fk'd this up and am hoping for some pointers

System:

  • PHP: 7.3.0alpha4
  • Laravel: 5.6.29
  • Session handling: file
  • OS: Ubuntu 18.04.1 LTS

Edit: I was able to check the _token key with a different laravel installation and the value changes with every page load. Looks to me there is a problem with session handling but I can't figure out where as it only has partial problems (writing something new to it obviously works but changing seems to run into a problem)

Edit2:

  • 写回答

1条回答 默认 最新

  • duanjia2415 2018-08-03 15:01
    关注

    as I can show, you are missing some details in session laravel documentation.

    first of all, if in your two methods (create , edit) you use redirect that will be better to use with for your flash session message like this example below :

     return redirect('route.name')->with('status', 'your message here');
    

    for more how with function work please check redirecting with flashed session data in laravel application

    next, within your blade template, you should create one test on that session variable, if it exists or not and then show up it. this code below will show how to that using the Session laravel Facade :

    @if(Session::has('key'))
        {{ Session::get('key') }}
    @endif
    

    you can find how to use session facade if you are curious on laravel 5.0 session documentation section

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来