Currently accepting inquiries for Webflow Websites and SEO Projects for Q3.

What is MVC?

November 12, 2021
May 1, 2023

What is MVC?

Matt Mitchell

Be it on a desktop or in a browser, the software helps us manage our lives in a seemingly endless number of ways. It's easy to take these things for granted, or at the very least underestimate how much goes into building the tools you use every day. Well today, we're going to give you a glimpse into how many software projects are structured to not only make things work as efficiently as possible but keep things manageable and scalable as the platform begins to grow in popularity. MVC, or Model-View-Controller, is one of the more popular methods being used today. But what exactly does it mean?

The Models

So every piece of software works with data, and it's typically going to exist in a database for it to be permanently stored. Models are essentially representations of our data. A lot of programs utilize what's called "Object Oriented Programming" concepts that make for better organization. So the "Model" portion of your software may take each row in a specific table, and represent each one as an object.A good example would be a "Users" table that stores all username information. You would have a "User" model (using something we developers call a class) that would turn each row in the user's table into an object. Each user object would have a name, password (though you wouldn't provide this, you would instead remove it from the object before being used, or exclude it from the get-go), some other additional information, and could each be called the same way. Each object that is made with the user class will have the same properties, though each object may have different values, and the same functionality that it could perform, such as changing it's information to more up-to-date data.Ideally, you would have a different class and Model for each table in your database, though that's not always going to apply. Exceptions could be if you have a database set up that uses "meta-data" tables like WordPress, or some tables that serve no purpose other than connect two other tables together. So let's say that in addition to your "User" table, you also have a "Project" table. Well, you need some way to connect a user to a project, typically by assigning or granting access. Here, you could easily have a basic table that connects these two but doesn't really hold "actual" information that the user will be using. No real need for a model/class for these.So that's a brief explanation of models, but all this is going to do is structure the data. Now we need to do something with the data, so we're a third of the way there. Next up...

The Views

So in a traditional web browsing experience (though this isn't the norm these days), each page you view is actually a file on the server. You can almost think of this as a word document that uses things like HTML tags to let the browser know how to display the data, and CSS will help tell the browser how it needs to look. For more on this, you can check out another post of ours where we go into details about how a web page is created.But this has started to change as technology improved. Now, you have the concept of 'routing', and we'll use WordPress as an example here. So when you go to a website, it looks for an "index" file with either an HTML or PHP file extension. This is the default homepage. But with WordPress, ALL pages go through the index file. The server basically turns the "/this/that" part of the URL from a directory path into a set of parameters. Routing your pages this way has a lot of different benefits, such as making it easier to temporarily constrict your site to an "under maintenance" page while you're working on it to prevent loss of data, but that's for another post. Routing helps the server know which "View" file it needs to retrieve. A huge benefit here is your views can be made up of different components that can be used across other views. This is extremely efficient, as it makes it to where changing one view or component will take effect across everywhere it's used. This makes maintaining the site all the easier.Another huge factor to keep in mind with Views is that they have no (or at least as little as possible) logic within them. So maybe you want to display and loop through some data that the view has received. Moreover, the Views will take the data from the Models and display them in a nice, presentable way that is hopefully user-friendly. But without any data-calling logic, as it's all done by the model, we need a way to connect the data provided by the models, to the views for the clients to see. This is where the last part finally comes in.

The Controllers

So models don't know about anything other than themselves, nor do the views. They both do one thing, independent of anything else. Remember when I mentioned having a database table that's only real purpose was to connect to other tables? That's kind of like a "Controller" in the MVC pattern. A controller, which could almost be looked at as a routing system like mentioned with WordPress, will basically look at the URL, treat it as a parameter, and find out what model(s) to use for data retrieval, and which views to load, and will put said data into said view. And honestly, that's about all their is to it. But let's have an example.Let's say you go to the Staff page of a website. The request will go to the server that will look for a route (this is using a controller) that matches what they typed into the address bar. Once a match is found, the controller will go to the model to get the data needed, and then insert that data into the view, that will, of course, present the data to the user.

That's all, Folks

And there you have it. You now understand a little more about how software, at least software using MVC architecture, is built. There are other ways to accomplish the same goals, but this is one of the most prominent software patterns and one that I'm most familiar with.  We hope this helps you appreciate the thought and work that goes into building the software you use day in and day out!

Related Articles