Where is the best place to access/write session variables in mvc,controller or model? Let's say we have a simple login model. this model has a method which gets username and password as parameters,validate these parameters and logins user by writing user id to the session. Is this the right way or should controller be responsible for writing user id to the session?
Asked
Active
Viewed 580 times
1
-
1The controller (or some service called by the controller) should be handling this. Generally, you never want a model accessing php superglobals (GET, POST, SESSION), always pass in the data required – Steve Mar 09 '15 at 15:18
-
@Steve It made more sense when you define session as superglobal. – Whiteley Mar 09 '15 at 15:22
-
@Steve You say that model should not access session but service can. Isn't the service part of the model layer? – Whiteley Mar 09 '15 at 15:25
2 Answers
0
I would query with the model, pass back to the controller and deal with the results there.
If you dont youll forget where you put things and quickly get in a right old mess.

DevDonkey
- 4,835
- 2
- 27
- 41
0
Where is the best place to access/write session variables in mvc,controller or model?
Model. Model layer consists of 3 parts (business logic, data mappers, services) - this fits perfectly to business logic component.
A great related article here: How should a model be structured in MVC?