MVC Frameworks (PHP in this case) [duplicate]
Possible Duplicate:
How to implement MVC from scratch in PHP?
Let me get this straight:
Model = your database table name
Controller = the middle man bet开发者_运维知识库ween user interaction and application logic
View = ??
So is a view a dynamic PHP page or a fragment of HTML?
I'm hoping i can get my head around MVC's quickly because i really want to implement them soon
The following explanation of MVC is taken from the codeigniter user guide.
The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
The View is the information that is being presented to a user. A View will normally be a web page. A view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of "page".
The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.
http://codeigniter.com/user_guide/overview/mvc.html
Edit: Example of what you may include in each entity
Controller (this file is targeted by the browser)
<?php
//Assuming that the database has already been connected
//Include the model
require('members_model.php');
//Get the posts made by a particular user from the database)
$posts = $this->members_model->get_posts($_POST['member_id']);
//Output the view
require('members_posts.php');
?>
Model
<?php
class members_model {
//Function to get all the posts made by a particular member
public function get_posts($member_id) {
$query = mysql_query("
SELECT *
FROM Posts
WHERE author = ".$member_id
);
return mysql_fetch_array($query);
}
}
?>
View
<html>
<head>
<title>All posts by member</title>
</head>
<body>
<?php foreach($posts as $post): ?>
<h1><?=$post['title']?></h1>
<span><?=$post['date']?></span>
<?=$post['body']?>
<?php endforeach; ?>
</body>
</html>
Obviously, this is an extremely simplified example but hopefully this gives you a rough idea of what should be going where.
The view knows how to display the data. The view knows you want to color some values red, and display these values as a list, and you want to use drag-and-drop to rearrange a list. It doesn't know where these values come from, so you can focus on the layout and user interaction.
The controller doesn't know any of that specific layout or behavior stuff. The controller only knows how to give the view values, how to save the order of the list, etc.
Models are the data and the DB. The controller intermediates between model and view. The views are typically (in PHP) HTML with some PHP mixed in. Think of the view as a template for the site. The view is responsible for rendering the data from the model into a suitable user interface.
The view is typically a system of different HTML/PHP pages (a template for the header, one for the footer, one for the body, and more for different aspects of the site).
EDIT here's a basic example of how you might write a view for a menu:
<div id="menu">
<ul>
<?php
// Say menu_items is an array mapping links to the text they should use
foreach($menu_items as $link => $text){
echo "<a href=\"$link\">$text</a>";
}
?>
</ul>
</div>
PHP is a template language in itself since it can be used with html , although there other template solution available such as smarty (which are designer friendly) .Bassicaly in your view you would be doing consing fil Foo.phtml
<p>Welcome <?= $this->username ?> </p>
Your View Class might have function loadView() inside which
class My_View
{
public $data;
public $view;
public function __set($property,$value)
{
$data[$preperty] = $value;
}
public function __get($property)
{
return $data[$preperty];
}
public function loadView()
{
ob_start();
include('Foo.phtml');
$this->view = $ob_get_clean();
}
public function __toString()
{
return $this->view;
}
}
In your controller class
class My_Controller {
public function __construct()
{
$view = new My_View();
$view->username = "jason bouren";
$view->loadView();
echo $view;
}
}
Most of the php MVC framework work this way . Inside Controller class you can create model object and use it . (All the code I have writter for explaining purpose only it has been not tested )
精彩评论