0

This is really a question about MVC standards and efficiency.

I have a User Controller, and want to retrieve data from my Session model. There's 2 ways I can go about doing this:

I can request the data directly from the Session model from within the User Controller. (UserController ---> SessionModel)

OR

I can ask the SessionController to ask the SessionModel for the data. (UserController ---> SessionController ---> SessionModel).

Both ways would seemingly work, and I'm not sure if either way is an 'incorrect' way of doing what I want. However, the second option seems somewhat inefficient and pointless. It just seems to be adding another layer in order to retrieve the data. However, I'm not sure if the first option goes against the rules of MVC as per say.

Any advice on this would be much appreciated.

1 Answer 1

1

Best practice is to call the model directly into the controller. So you should call your SessionModel inside the UserController. In this way the loop it's quite short and efficient.

In my opinion the best way to do that is to introduce another layer the "services" if you're working with a Framework (such as Symphony) you can use dependency injection pattern to resolve this type of problems.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your opinions. I'm not actually using any framework, I've started from scratch, I should have said that. Could you explain a bit more to me with this 'services' layer please? What could be included in it, and how could it work? Apologies, I don't quite understand what you mean by it :)
To understand the service you should read this article [link] (sitepoint.com/an-introduction-to-services) which explain how services works, how to implement them and how they are integrated into MVC model. Using a framework it's more easily just because the logic behind it's already available "ready to use". Hope to be helpful...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.