1

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.
Community
  • 1
  • 1

1 Answers1

0

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.

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
  • Thank you for the answer, I've already read the full Session documentation on the Laravel :) The problem, I'd like to know where is the best place to store them. By the way, `Session::put('hello','world'); Session::save();` syntax is no longer submitted in the newest Laravel docs you linked :) – Puka Unknown Aug 08 '18 at 10:03
  • @PukaUnknown session()->put() does exactly the same, it calls Session::put under the hood by accessing the static variable. but that's semantics. You call these where-ever you have need for the values stored in the session. In a user authentication module you call it in that module. For a username display you call it in a template or in the controller rendering the template, for a shopping cart, you call it in the shopping cart render module, etc... – Tschallacka Aug 08 '18 at 10:05
  • By saying `module`, you mean inside the controller? I would have to load that controller every time, maybe using built-in Service Provider `boot` function would be a better approach? – Puka Unknown Aug 08 '18 at 10:07
  • You have a controller, that renders several elements. Sometimes you wish to re-use elements, so you build your own render code for it so you can re-use it and then just call `$shoppingRender = new ShoppingCartRender(); return $shoppingRender->render();` that's what I call a module. You really don't need to call it in boot() or anywhere just to put it in a static class. The Session class/helper function in itself is static and a singleton and available from EVERYWHERE. Just call it when and where it's needed. Don't overcomplicate things. keep it simple. – Tschallacka Aug 08 '18 at 10:11
  • Yea, thanks, I simply would like to learn the best practices, so in the future, it's gonna be simple :) But then I'll have to call that controller on boot while using the `boot` function, I wouldn't need that. Am I right? – Puka Unknown Aug 08 '18 at 10:18
  • No, as long as laravel is loaded and the session class can read and write variables you can use it anywhere you want. – Tschallacka Aug 08 '18 at 10:52
  • Where would you put them in my situation? I understand "anywhere you want" but it's not very helpful. – Puka Unknown Aug 08 '18 at 11:01
  • @PukaUnknown you have not shown code or what your execution flow is. First wordpress then laravel? first laravel then wordpress? first generic, then laravel, then wordpress then something else? is laravel used as a helper in a wordpress plugin? does laravel interface with wordpress to get data out? you haven't provided what kind of data you store in session or what you want to do with it. without that information i really can't say much. – Tschallacka Aug 08 '18 at 11:02
  • Oh, ok, sorry about that. It's the Laravel integrated to the WordPress (but it's a different folder - **laravel**: http://prntscr.com/kg5f5e. Inside that folder, you can see a basic file structure of the Laravel: http://prntscr.com/kg5g3x. It's not a plugin. I've loaded WP functions (wp-load.php file) in the Laravel Service Provider here: http://prntscr.com/kg5ijm. No fancy stuff here :) The session data I'd like to store is simple. Basic strings and integers :) – Puka Unknown Aug 08 '18 at 11:29
  • @PukaUnknown do you only need the session data in laravel? or do you need the session data in wordpress(running as wordpress instance) as well? – Tschallacka Aug 08 '18 at 11:33
  • Nice question! :) Yea, I'd need it only for the Laravel. There is a .htaccess file, which redirects to the Laravel public path. – Puka Unknown Aug 08 '18 at 11:35
  • @PukaUnknown Then you can just go with the `Session::get()` or `session()->get()` method without needing hacks to get the values within wordpress as well. – Tschallacka Aug 08 '18 at 11:46
  • I've been using `session('...')` to get the value and `session(['...' => '...'])` to set it but that's just a shortcut. Where should I place session data? I'm sorry if it feels like you've already my question. I'd like to know what's the best way in this situation. Maybe it's very valuable to others but maybe someone here also trying to connect WP with the Laravel and have the same question :) Why putting this data in the Service Provider is bad? Didn't quite understand why it's not a good way? - "I'll have to call that controller on boot while using the boot function, I wouldn't need that." – Puka Unknown Aug 08 '18 at 11:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177632/discussion-between-tschallacka-and-puka-unknown). – Tschallacka Aug 08 '18 at 11:56