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:
- Use Laravel Service Provider and insert session variables inside the
boot
function. - Use Laravel Middleware functionality, however, not sure how to implement this.