What is MVC in PHP?
I am trying to understand the MVC pattern. This is what I think MV:
Model:
<?php
if($a == 2){
$variable = 'two';
}
else{
$variable = 'not two';
}
$this->output->addContent($variable);
$this->output->displayContent();
?>
View:
<?php
class output{
private $content;
public function addContent($var){
$this->content = 'The variable is '.$var;
}
public function displayContent(){
include 'header.php';
echo $content;
include 'footer.php';
}
}
?>
Is it correct? If so, which controller?
a source to share
The controller is your logic, the model is your data, and the view is your output.
So this is the controller:
$model = new UserDB();
$user = $model->getUser("Chacha102");
$view = new UserPage($user->getName(), $user->getEmail());
echo $view->getHTML();
Model is a UserDB class that will give me my data. The view is a UserPage that I pass data from the model and then displays this page.
As you can see, in this example, the controller doesn't do much, because you just get the custom data and display it. This is the beauty of MVC. The controller doesn't have to deal with SQL SQL or HTML stuff, it just grabs the data and passes it to the view.
In addition, the view knows nothing about the model and the model knows nothing about the view. This way you can implement the implementation without affecting the other.
Referring more to your example, you have the correct view, but you have mixed your controller and model.
You can reduce this:
Controller:
$model = new NumberToWord();
$word = $model->getWord($a);
$this->output->addContent($word);
$this->output->displayContent();
Model:
class NumberToWord{
public function getWord($number)
{
if($number == 2){
return 'two';
}
else{
return 'not two';
}
}
}
And keep the same result
a source to share
Controllers receive user requests - usually there is some kind of router that takes the url and redirects the request to the appropriate controller method.
Models are used by the controller to query data to / from the database (or other data source).
Views are called from the controller to render the actual HTML output.
a source to share
In your example, this is more like dividing the controller into model and view.
- Model: Business logic / rules and usually some kind of database for object relational mapping
- Controller: Responds to URL requests by pulling in the appropriate models and views to generate output.
- View: The visual structure on which the result will be displayed. Typically a "dumb" component.
This can be confusing when you first come across MVC for a web application, mainly because most web frameworks are not MVC at all, but bear a much closer resemblance to PAC . In other words, the model and the view are not talking, but are two elements grouped by context that the Controller understands from a given request. Check out Larry Garfield's excellent commentary on the matter:
http://www.garfieldtech.com/blog/mvc-vs-pac
Also, if you are interested in the MVC development pattern, I suggest you download one of the many frameworks and go through a tutorial or two. Kohana, CodeIgnitor, CakePHP and Zend should be enough to get Google-a-thon up and running!
a source to share
Zend Framework: Surviving The Deep End has several helpful sections explaining MVC. Check out MCV Intro and especially this seciton on the model .
There are many interpretations of the Model, but for many programmers, the Model equates to data access, a misconception that most frameworks inadvertently promote without explicitly acknowledging that they do not provide complete models. In our flooded community, in many words, many frameworks leave the definition of a model unclear and hidden in their documentation.
To answer "where is the controller":
Controllers should only define application behavior in the sense that they map user interface input to calls in the Model and handle client interactions, but beyond this role, it should be clear that all other application logic is contained in the Model. Controllers are humble creatures with minimal code that simply set up the scene and allow you to work in an organized way in the environment in which your application runs.
I think you'll read this well (and its links to other articles and books).
a source to share
Here is a very basic MVC example using PHP. Only one router is missing. He chooses one of the controllers to do the job. We only have one controller, a customer.
If we compare this to 3 levels
Model: Database View: Client Server: Controller Router: It chooses a controller
When you select something from the application in the web browser, the request is sent to the router, from the router to the controller. The controller asks the model and pretends. The view is displayed for you.
Only the model can talk to the controller back and forth.
1- Model.php
<?php
class Model
{
private $con=null;
private $r=null;
function connect()
{
$host="localhost";
$db="mis";
$user="root";
$pass="";
$this->con=mysqli_connect($host,$user,$pass) or die(mysqli_error());
if(!$this->con){
echo die(mysqli_error());
}
else mysqli_select_db($this->con,$db);
}
function select_all()
{
$this->connect();
$sql="select * from customers";
$this->r=mysqli_query($this->con,$sql) or die(mysqli_error());
return $this->r;
}
function display_all()
{
$i=0;
echo "aaaaaaaaaa";
$this->r=$this->select_all();
while($q=mysqli_fetch_array($this->r))
{
$i++;
echo $i."-Id:".$q['id']."</br>";
echo $i."-Name:".$q['name']."</br>";
echo $i."-Phone:".$q['phone']."</br></br>";
}
}
}
?>
2. Controller: There may may be many controllers.
<?php
class Customers extends Model
{
function select_all1()
{
//echo "aaaaaaa";
$this->display_all();
}
}
?>
3. View: There may be many views
<?php
include("model.php");
include("customers.php");
?>
<html>
<head><title>Customers</title></head>
<body>
<?php
$c=new Customers();
echo $c->select_all1();
?>
</body>
</html>
a source to share