douyue6520 2018-08-08 09:49
浏览 58

我应该在哪里存储基于MVC模式的Laravel Session数据?

I'm working with the Laravel integrated to the WordPress and struggling to understand where should I put the session data based on the MVC design pattern?

Back in the day, I used to put everything inside the view (header.php and footer.php) files and after some time, it became a mess, complete mess.

As written here:

As MVC I use CodeIgniter, so I don't know if this can be true for your specific environment, but I usually set session values from the controller. It is possible to do it even in view but the correct way is to keep code in controller (as keeping database stuff in models).

In the controller, you can use standard php $_SESSION array or, it it exists, your framework session class.

Yea, I understand it's a good practice to not mess around with the view and put session variables inside the controller. Here is the problem:

As I'm using the WordPress, the goal is to have a place where the session variables are always loaded, doesn't matter if I changed the theme or anything, they should stay in the Laravel backend.

Without any testing, I could think about a couple option:

  1. Use Laravel Service Provider and insert session variables inside the boot function.
  2. Use Laravel Middleware functionality, however, not sure how to implement this.
  • 写回答

1条回答 默认 最新

  • dqsk4643 2018-08-08 10:00
    关注

    You can use the laravel https://laravel.com/docs/5.6/session Session helper.

    Then you can just do Session::put('hello','world'); Session::save(); and retrieve it with Session::get('hello'); You can do this anywhere you'd like, as long as you remember to save the session after putting things in it, modifying things or removing things.

    As long as Laravel is loaded and the domain has the laravel session cookie, you can access them.

    评论

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站