95 2
514 times

I sort of understand what MVC is in theory. Model, View and Controller and CodeIgniter, Laravel, CakePHP and those sort of libraries (would that define them?) use a form of MVC or a derivative. I've actually used Laravel (the only "library" I was able to figure out how to work) but I have no idea how it works.

Now though, I've gotten the idea that the user, when typing (as an example) lands on the Controller which then combines the correct Model to it's View and displays the formatted results to the user. Is that the correct understanding of MVC?

For some reason, I've being unable to completely understand the concept entirely regardless of how many tutorials I've read on the topic and I've being trying for quite some time (though not consistently... haven't being coding for a few years now).

  • 0
    You generally have the right idea, however, when visiting a website you don't necessarily land on the controller, but typically most will setup routes to load a Controller's method that handles the logic. — Brian Wozeniak
  • 0
    I don't understand exactly what routes are, how they work and how they're setup. I understand the general picture but don't know the intricate details of it. — Bogey
add a comment

1 Answer

  • Votes
  • Oldest
  • Latest

You have already properly identified the meaning of MVC in your question, Model-View-Controller, but essentially it is a design pattern that is used in programming and web development frameworks like you mentioned. You might also say it's an architectural pattern since it sort of forms the basis of how your application generally is pieced together.

If you have heard of the SOLID principle, MVC follows the "S" principle by separating responsibilities. The Model contains the data, the view renders the elements that interact with the user, and the controller ensures that the sequence of steps occurs correctly and pieces everything together.

Does that make it all crystal clear to you now? Probably not, you might be exactly where you were before with your understanding. Maybe a better question would be, What is the point of MVC, and how will it help me?

I think where this pattern excels is when you have rather complex systems, especially systems that you might reuse in different places. A common model for instance is a model representing the User. With this model, you would probably be able to obtain all sorts of data, probably much from the database where it's stored, but you may also have other logic in the model that does calculations or has to run through logic before returning the value that is used. You can then load up this Model in a controller and send some of its data to a view that is presented to someone visiting your website. The controller itself would be rather simple, it would ask for the User Model and then would send any data obtained from this model to the view which would render the HTML and the data together.

Now imagine if you didn't use the MVC pattern, and instead, you had a single method in a class for each webpage on your website that ran through logic line-by-line. This method would have to do quite a bit of work, it would have to have logic in place to bring in the user's data (perhaps a unique database query), it would have to run any calculations, and it would need to somehow have logic in place for HTML and inserting any dynamic data in that HTML where it belongs. The old Ozzu which ran on PHPBB did exactly this sort of procedural programming, and it was often frustrating to work with. I always feared making certain changes because I was not sure how they might affect other areas. Everything was very coupled together.

So by using MVC, you will:

  1. Promote separation of responsibilities: Allows you to help make the code easier to maintain and extend since everything is decoupled.
  2. Support code reusability and scalability: Can easily change the view or model without affecting other components. Perhaps you want a view for when people visit the web page, but you also want a view for those accessing the data via an API. No problem, you can easily reuse the components.
  3. Maintain better organization of code: Always helpful, but even more so if you have a large or complex application.

Finally just a note about routing. Most MVC systems used with web applications will have some sort of routing system so that you can send (route) requests to a specific controller and method that will handle it. Laravel for instance sends everything through a routing system first so that you can do exactly this (if you want). The image below shows what the MVC model looks like with the routing portion included:

Diagram of the MVC Design Pattern

In a nutshell, it's just a way to figure out what controller/method is responsible for returning what the user asked for. From there the controller's method will get all of the data the view needs via models, get its rendered result from the view (HTML for a web page, JSON for an API response, etc), and then send it back to the user who requested it.

add a comment