嗯,我不是一個龐大的框架人,但我一直喜歡我聽到的關於整個MVC運動的內容,所以我想我會嘗試創建一個簡單的應用程序在我的選擇語言(PHP)請批評我在PHP MVC上的第一次嘗試
所以我想問題是:我哪裏出錯了?我知道有很多關於控制器/模型應該如何肥胖的爭論,所以希望我們可以避免這種情況,但是我特別好奇你對我如何適合數據的想法。
另外我買了一個域做一些測試,所以如果你想看到它在行動,你可以去www.omgmvc.com
首先,這裏是我的數據庫模式:
CREATE TABLE `movies` (
`id` int(11) NOT NULL auto_increment,
`movie_name` varchar(255) NOT NULL,
`release_date` date NOT NULL,
`directors_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `movies` VALUES (1,'Star Wars', '1977-05-25', 'George Lucas');
INSERT INTO `movies` VALUES (2,'The Godfather', '1972-03-24', 'Francis Ford Coppola');
INSERT INTO `movies` VALUES (3,'The Dark Knight', '2008-07-18', 'Christopher Nolan');
而這裏的文件:
的index.php(控制器)
<?php
include('datatier.php');
include('models/m_movie.php');
if (isset($_GET['movie']) && is_numeric($_GET['movie']))
{
$movie = new Movie($_GET['movie']);
if ($movie->id > 0)
{
include('views/v_movie.php');
}
else
{
echo 'Movie Not Found';
}
}
else
{
$movies = Movie::get_all();
include('views/v_list.php');
}
?>
datatier.php(數據層)
<?php
class DataTier
{
private $database;
function __construct()
{
$this->connect();
}
function __destruct()
{
$this->disconnect();
}
function connect()
{
$this->database = new PDO('mysql:host=localhost;dbname=dbname','username','password');
}
function disconnect()
{
$this->database = null;
}
function get_all_from_database($type)
{
$database = new PDO('mysql:host=localhost;dbname=dbname','username','password');
switch ($type)
{
case 'movie':
$query = 'SELECT id FROM movies';
break;
}
$movies = array();
foreach ($database->query($query) as $results)
{
$movies[sizeof($movies)] = new Movie($results['id']);
}
$database = null;
return $movies;
}
function get_from_database($type,$id)
{
switch ($type)
{
case 'movie':
$query = 'SELECT movie_name,release_date,directors_name FROM movies WHERE id=?';
break;
}
$database_call = $this->database->prepare($query);
$database_call->execute(array($id));
if ($database_call->rowCount() > 0)
{
return $database_call->fetch();
}
else
{
return array();
}
}
}
?>
模型/ m_movie.php(模型)
<?php
class Movie extends DataTier
{
public $id;
public $movie_name;
public $release_date;
public $directors_name;
function __construct($id)
{
parent::connect();
$results = parent::get_from_database('movie',$id);
if ($results == array())
{
$this->id = 0;
}
else
{
$this->id = $id;
$this->movie_name = $results['movie_name'];
$this->release_date = $results['release_date'];
$this->directors_name = $results['directors_name'];
}
}
function __destruct()
{
parent::disconnect();
}
static function get_all()
{
$results = parent::get_all_from_database('movie');
return $results;
}
}
?>
組的意見/ v_list.php(圖)
<html>
<head>
<title>Movie List</title>
</head>
<body>
<table border="1" cellpadding="5" cellspacing="5">
<thead>
<tr>
<th>Movie Name</th>
<th>Directors Name</th>
<th>Release Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($movies as $movie) { ?>
<tr>
<td><a href="/?movie=<?php echo $movie->id; ?>"><?php echo $movie->movie_name; ?></a></td>
<td><?php echo $movie->directors_name; ?></td>
<td><?php echo $movie->release_date; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
意見v_movie.php(圖)
<html>
<head>
<title><?php echo $movie->movie_name; ?></title>
</head>
<body>
<h1><?php echo $movie->movie_name; ?></h1>
<h2>Directed by <?php echo $movie->directors_name; ?></h2>
<h3>Released <?php echo $movie->release_date; ?></h3>
</body>
</html>
不錯的問題,但我建議這真的屬於codereview。 – GordonM 2012-03-27 19:01:14