Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC tutorial that doesn't rely on a framework? [closed]

I want to learn MVC "architecture pattern" but I don't want to jump into a framework like Rails or Django just yet. I want to understand the concept first and write some simple code in my currently familiar environment, which happens to be PHP/HTML/CSS/MySQL. I don't necessarily need a tutorial that is based on PHP, as I do understand a lot of different languages. And I don't want to have to install any frameworks or APIs or libraries. I just want to learn how to think in MVC and apply it to my projects. Any suggestions?

like image 345
Ixion Avatar asked Sep 30 '08 07:09

Ixion


People also ask

Is MVC architecture obsolete?

Is the framework outdated? ASP.NET MVC is no longer in active development. The last version update was in November 2018. Despite this, a lot of projects are using ASP.NET MVC for web solution development.

Is flask an MVC?

But Framework flask still doesn't use the Model View Controller (MVC) method. So there are weaknesses including the difficulty in making repetitive codes and source code management. Therefore, to make it easier for web developers who use framework flask, MVC is needed in the framework flask.

Why is MVC so popular?

UI changes are still very easy, perhaps even easier. In MVC, the Controller and View tend to mesh together. Layers creates a strict separation. Both Layers are black boxes, free to vary independently in implementation.


2 Answers

Almost every framework does MVC differently, so you might end up getting even more confused. The general principles of MVC are very simple: "Model is state; view reacts to model; controller reacts to view; controller changes model". The model, view and controller are concepts - they are whatever you feel them to be. Classes, bunches of classes, instances of classes with XML configuration files, you name it.

I actually think that about covers the basic principles. Without a framework, you'd not get much further. What matters is how a particular framework defines model, view and controller and their interactions.

like image 114
Sander Avatar answered Sep 17 '22 13:09

Sander


MVC is basically just splitting up your code into a Model, which deals with the data, a View which displays the data, and a Controller which passes data from the Model to the View.

It's nothing you need an API or framework for, it's just a way of splitting up your code. The reason many frameworks use it is because it's a very simple concept, it works well for many things (it fits webpages perfectly), and is fairly flexible (for example, with Rails, you could do everything in your view, or model/controller, if you so-desired..)

A quick example in python, of an example MVC structured Python script. Not necessarily "best practices", but it works, and is fairly simple:

class Model:     def get_post(self, id):         # Would query database, perhaps         return {"title": "A test", "body": "An example.."}  class Controller:     def __init__(self):         self.model = Model()         self.view = View()      def main(self):         post = self.model.get_post(1)         self.view.display(post)  class View:     def display(self, item):         print "<h1>%(title)s</h1>\n%(body)s" % item  c = Controller() c.main() 
like image 37
dbr Avatar answered Sep 20 '22 13:09

dbr