MVC - How does it work in the real world?

I read a lot of trivial MVC examples for calculators and thermometers, but I can not imagine a diagram for real applications.

Suppose you have a more complex scenario. Let's say you have a shopping cart on a site that requires users to log in before adding to the cart. Firstly, the user sees the product page (/ product / part) and clicks on adding an item (/ cart / add / 207366). The user has not logged in yet, so they need to visit the login page (/ user / login), and then, being smart about the flow, they transfer them to the shopping cart window (/ cart / list). From there, they can return to the original product details page to continue shopping.

Let's say we have 3 database tables: users, usercart and products. What is the model (s) in this situation? Will all this thread be encapsulated in the addProductToCartFlow function of the ShoppingCart model? This would be a bit messy, as he would need to access the user table for login / authentication and access the product table in order to pull the product details / price into the cart.

Instead, will you say that the ShoppingCart model is SELF-CONTAINING and only applies to adding items, deleting items, etc. from the basket? The "logic" of the user entering the system would be verified elsewhere: perhaps in the controller itself? This would make the controllers very BUSY with a rather "business logic", for example, checking the user login, checking if the shopping cart is etc., and the model just becomes a beautiful name for the database table.

Or maybe the fact of logging in or logging out is part of the UserAuthentication model that deals with such functions. Or maybe we need a UserPageState model to tell us if the user should be on the login page or on the cart page or on the product details page?

What is the best MVC design for this situation, in your opinion?

+4
source share
3 answers

Your models, in fact, are business objects. In most cases, you will have ShoppingCarts, Users, and Items (perhaps one basket per customer, but who can say that?). You will have controllers that control the flow - the controller method for / cart / add / 207366 will check if the user is allowed and pass them to the controller for / login if not. The input controller must be smart enough to pass the correct information back to the controller for / cart / add / 207366, which then has to add the item to the cart.

The controllers would call Cart.AddItem (), but the business logic was contained within the model of the shopping cart - it could look for the price of the goods, the preferred discount for the client based on the User, etc. Controllers will not know or care about this. Controllers need to know if the user is registered (if it matters to the application), as this affects their work (determining which view to visualize). They do not need to know whether the customer is preferable or to hold a loan or any other conditions for business logic. All this is handled by models.

+3
source

I will have a model for each table on my simple sites. I can use any number of models in my controllers. In more complex examples where the client and the cart are not separate, I have a model that combines them together with methods that both tables know.

You have many models, very subtle controllers, and only HTML in your views. Your controllers should have many if statements that receive results from the controllers. Your views may “hang” on this cycle and repeat the results, but no other logic. There are no HTML tags anywhere except your views.

To answer your question, you may have the following models.

  • Users [get to user table]
  • Cart [Enter the basket table]
  • product table and user table]
  • Products [fall into the product table]
  • Orders [table of users, carts and products (as a simplified simplified example)]
  • PageState is a model that tracks the status of your users on your site.

You have the following controllers

  • Basket
  • of users
  • Products

Controllers can be very long, but each action is small, with a specific task. I think it is normal to have a controller with 10 actions if these actions are good and short.

The models that trigger these actions can be quite long and usually need to take into account business logic. I also use helper classes to do things that are not business logic, but are still critical. Think about security and verification.

And a whole bullet of glances. Presentations for the basket, for the login window, for a product summary, a detailed description of the product, each phase of verification, receipt, views for sending by e-mail in HTML format, a list of categories, menus, footers. Many and many species.

You would like to have some ApplicationClass that all your controllers inherit so you can do something over and over again.

You mentioned php, so you have the views that make up your template, but if you made ASP, you will have a main page that will run partial views. I don’t know exactly what you are doing in Ruby or Python, but it does.

0
source

I think that your concern about busy controllers should not worry: what is their purpose. The cart controller, which has an add action, will rely on the user controller or authentication assistant to find out if the user is logged on. The above two answers were very well reflected.

Also, instead of creating another model for the pages, I would just use a session variable or another field in the table. Another model will really just complicate the situation, and in fact, it does not represent an entity that should be autonomous. When do you see the need for a stack of visited pages? This is actually just the URL string the user came in to get the login form, which you can track in much more intensive code and memory mode with session variables. Just have addItem () to check if it is registered and if it is not redirected to the login page and saves the current request. Then after logging in, check if the forwarding variable is set, and if so, go to it and reset. I do not see the need for anything more complicated than this.

Also, it looks like your pastebin code was CakePHP. A good choice. If you want, use the baking console and create the forest code. It does so much work for you, and just leaves good things for you.

0
source

All Articles