List

MVC is a term thrown around lots of software development projects. Laravel, Zend, CodeIgniter, Ruby on Rails, Django, iOS, and lots of other frameworks rely on the MVC architecture. But what exactly is MVC? MVC is not a framework of itself, but instead is an architecture for your application and more like a way of thinking. I will break down what MVC is, using an example simple blog with authors and posts.

Now back to MVC, MVC stands for the three components that make it up:

M – Model.

The Model is the data of your application. All information used by your application should interact with its corresponding model.

Models are essentially custom classes relevant to the data you have. For example, my Author model might look similar to this:

Our Author class might look something like this:

class Author extends Model
{
    protected $id;

    protected $displayName;

    public function initialize()
    {
        $this->hasMany('id', 'Post', 'author_id');
    }

    public function getId()
    {
        return $this->id;
    }

    public function setDisplayName($displayName)
    {
        $this->displayName = $displayName;
    }

    public function getDisplayName()
    {
        return $this->displayName;
    }
}

and our Post class:

class Post extends Model
{
    protected $id;

    protected $author_id;

    protected $body;

    protected $title;

    public function initialize()
    {
        $this->belongsTo(
            "author_id",
            "Author",
            "id"
        );
    }

    public function getId()
    {
        return $this->id;
    }

    public function setBody($body)
    {
        $this->body = $body;
    }

    public function getBody()
    {
        return $this->body;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

}

Models also will reference the relationships between different data, and this will be specific depending on your framework. This example above uses the syntax of Phalcon, but other frameworks will use similar methods of defining the relationships between models. Phalcon or your framework will also handle DB queries for you (notice how we extend the class “Model” in both cases), but if you do not have DB support already, you will want to define the DB interactions inside your parent Model class.

V – View.

The view is strictly the response given to the client. Most frameworks will utilize a templating engine to render these views, making the creation of these views easier.

Here would be an example view for our blog (using the Twig template markup):

<html>
	<head>
		<title>{{ title }}</title>
	</head>
	<body>
		<h2>{{ title }}</h2>
		<p>{{ post }}</p>
	</body>
</html>

It is important to note the view contains nothing more than a template of the final response. Views should never contain application logic and should never interface with models directly. Doing so is bad practice and can create more headaches down the line when changing app logic. Keep all this logic in the controller.

C – Controller.

The controller is the brain and guts of your application. The controller is responsible for processing user input, fetching/creating the appropriate models, process your application logic, grabs the view, renders it, and ships it to the user. The controller is the only component that can interact with models and views. It is not appropriate to reference a Model from within a view or return a view from a model. All of this must remain in the controller.

For example, I may have a controller labeled PostController, which checks for a post ID, finds it, and renders the view with the content from the model.

class PostsController extends Controller
    public function mainAction($id = null)
    {
 		$post = Post::findFirstById($id);
 		if($post) {
            $contents = $this
                ->viewSimple
                ->render(
                    'pages/view',
                    [
                        'title'      => $post->getTitle(),
                        'post'       => $post->getBody()
                    ]
                );
	        $this->response->setContent($contents);
	        return $this->response;
 		} else {
	        return $this->mainAction('404');
 		}
    }

I hope this quick dive into MVC has been helpful and clears up any confusion on what MVC is and how it works!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Posts

1 2 3 4
February 28th, 2018

Concluding the February Challenge

Writing every day for the month of February has been an experience, and has changed my perspective on writing tremendously. […]

February 27th, 2018

Cloudflare2DB

Cloudflare stats are very useful, but they expire and can’t be tracked over a long period of time. That’s why […]

February 26th, 2018

Nightly Database Backups to Google Drive

If you have a Google Apps account (a business account with more than 5 users or one provided by your […]

February 25th, 2018

Sublime Text

Sublime Text is a really powerful text editor and includes tons of functionality relevant to programming. I will dive into […]

February 24th, 2018

Great Resources for Student Developers

Being enrolled in a university comes with its pros and cons (a topic itself worthy of many other blog posts), […]

February 23rd, 2018

What is MVC?

MVC is a term thrown around lots of software development projects. Laravel, Zend, CodeIgniter, Ruby on Rails, Django, iOS, and […]

February 22nd, 2018

Getting Started With Phalcon PHP

To get started with Phalcon, I already have my server stack setup with Ubuntu 16.04, Percona MySQL, Nginx, and PHP […]

February 21st, 2018

What is Phalcon?

Phalcon is a modern PHP framework with the typical feature set you’d expect. Database ORM, error handling, MVC architecture, and […]

February 20th, 2018

Setting Up a PHP Development Environment

If you do PHP development, moving to a PHP IDE is extremely beneficial especially when debugging. PHPStorm is a great […]