Sunday, August 26, 2012

MVC Introduction

MVC stands for Model - View - Controller and these three are the building blocks that make up the MVC pattern. A website which uses MVC will be mainly based on the communication that happens between these three components.

What is a Controller?
Controllers will be the receivers and dispatchers. When a user sends a request by clicking a link or button then the request is handed over to the controller (There can be many controllers in the website and the correct controller to handle a particular request will be identified by the URL pattern) the controller will have a set of methods(show, index, etc...) defined and one of these methods will do the necessary tasks to generate a response. Basically, Controller will be the intermediary who will communicate with the Modal and generate an appropriate View and send that to the client.

What is a Model?
Model handles the database interactions for the application. The Model will return the data which is requested by the Controller.

What is a View?
View is the HTML output that a user sees when he makes a page request. This view will be rendered based on the Controllers' request and by the data that is passed in by the Model.

As explained earlier the MVC architecture defines a clear isolation between the Presentation layer and the Data access layer. The presentation will be in the View and the Data will be in the Model. These two never interact directly with each other. Just look at the steps below to get an idea of how a request/response cycle works in MVC, the numbers indicate the order of execution.

1.0. User request arrives
1.1. Controller receives the request
1.2. Pass it to the correct controller method
1.3. Fetch data from the database by initiating the appropriate Model
1.4. Pass the data from 1.3 to the View and generate the HTML View.
1.5. Send the generated HTML as the response.

No comments:

Post a Comment

Mixed Content: The page at xxx was loaded over HTTPS, but requested an insecure

 Mixed Content: The page at ' https ://www.test.com/signup.aspx' was loaded over HTTPS, but requested an insecure script ' http ...