MVC in PHP
Upcoming SlideShare
Loading in...5
×
 

MVC in PHP

on

  • 2,470 views

 

Statistics

Views

Total Views
2,470
Views on SlideShare
2,470
Embed Views
0

Actions

Likes
2
Downloads
69
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

MVC in PHP MVC in PHP Document Transcript

  • MVC in PHPIntroductionMVC stands for Model-View-Controller. Model means data, View means representation andController means business logic. MVC was originally described in terms of a design patternfor use with Smalltalk by Trygve Reenskaug in 1979. MVC is a software architecture or wecan say that MVC is a design pattern or MVC is a framework. The MVC pattern is a processof breaking an application into three parts: the model, the view, and the controller. ADesign pattern is a code structure that allows common coding frameworks to be replicatedquickly.MVC is used in software development; the fundamental principle of MVC is based on theidea that the logic of an application and presentation are separated from each other i.e.MVC is simply a better way of separating the logic of your application from the display. MVCis an design pattern which is used in software programming. In MVC, the model representsdata of the application; the view represents presentation logic and the controller representsall our business logic and manages the communication of data and the business rules. TheMVC architecture has been used in many programming languages and technologies such asJava, .NET and PHP etc.The purpose of the MVC pattern is to separate the model from the view so that changes tothe view can be implemented, or even additional views created, without having to refactorthe model.A simple way to think of this would be to consider the following:  Model handles all our database logic.  Using the model, we connect to our database and provide an abstraction layer.  A user interacts with the view - by clicking on a link or submitting a form.  The View checks the state of the Model and responds accordingly.  View represents our presentation logic i.e our HTML/XML/ code.  The Controller handles the user input, and transfers the information to the model.  The Model receives the information and updates its state. www.vineetsaini.wordpress.com
  •  The Controller represents all our business logic i.e. all our ifs and else.ExampleA bike is a real-world example of MVC. With a bike you have two views: the interior and theexterior. Both take input from the controller: the driver. The brakes, wheel and othercontrols represent the model: they take input from the controller (driver) and hand them offto the views (interior/exterior) for presentation.ModelModel is the first part of the MVC pattern. Model handles all our database logic. The Modelmay not necessarily have a persistent data store (database), but if it does it may access itthrough a separate Data Access Object (DAO). The main features of a Model are as follows:  A model represents data e.g. a database table.  The model manages the behavior and data of the application domain.  The model represents data and the business rules that govern access to and updates of this data.  The model is the piece of data that represents the state and low-level behavior of the component.  Model manages the state. The model has no specific knowledge of either its controllers or its views. www.vineetsaini.wordpress.com
  •  A model can have more than one view.View  The view is the piece that manages the display of the state represented by the model.  View represents our presentation logic i.e. our HTML/XML/ code.  A view is some form of visualization of the state of the model.  The view manages the graphical and textual output.  It accesses enterprise data through the model and specifies how that data should be presented.  A view attaches to a model and renders its contents to the display surface.Controller  The Controller represents all our business logic.  Using a controller we can change the state of the model.  A Controller accepts input from the user and instructs the model and view to perform actions based on that input.  The controller translates interactions with the view into actions to be performed by the model.  The Controller provides the mechanism by which changes are made to the state of the model.  The Controller handles the HTTP GET and POST requests.There are many frameworks available which follow the MVC pattern in PHP. Like as ZendStudio framework, CodeIgniter framework, PHP Smart etc. In the next article I will explainthe CodeIgniter framework which follows the MVC pattern.ConclusionSo in this article you saw an introduction to MVC in PHP. In the next article you will see theMVC pattern used in the CodeIgniter framework. www.vineetsaini.wordpress.com