Herkes PHP MVC yaklaşımı Hello Word çok basit bir örnek verebilir misiniz?
İşte bazı "Merhaba Dünya" MVC bulunuyor:
function get_users() {
return array(
'Foo',
'Bar',
'Baz',
);
}
function users_template($users) {
$html = '<ul>';
foreach ($users as $user) {
$html .= "<li>$user</li>";
}
$html .= '</ul>';
return $html;
}
function list_users() {
$users = get_users();
echo users_template($users);
}
Ana fikir data presentation (görüş) den data access (model) ayrı tutmaktır. Denetleyici birlikte iki kablolama daha fazla yapıyor olmalıdır.
Burada en temel örnektir. Index.php dosyası denetleyicisi, modele bazı verileri alır, sonra bir görünüm dosyası ile HTML içerir.
/* index.php?section=articles&id=3 */
// includes functions for getting data from database
include 'model.php';
$section = $_GET['section'];
$id = $_GET['id'];
switch ( $section )
{
case 'articles':
$article = getArticle( $id );
include 'article.view.php';
}
.
/* article.view.php */
<html>
<head>
<title><?=$article['title']?></title>
</head>
<body>
<h1><?=$article['title']?></h1>
<p><?=$article['intro']?></p>
<?=$article['content']?>
</body>
</html>
QuickStart of Zend Framework Zend Framework dayanarak, "basit bir uygulama" (not an "Hello World", but not much more -- and using MVC for an "Hello World" application is a bit like using a nuclear bomb to kill a bug) bir çok kötü değil örnek olduğunu ve MVC kullanarak.
Hala devam eden çalışma, ama yine de okumak ilginç bir - Eğer daha uzağa biraz olsun istiyorsanız sonra, size elektronik kitap Survive The Deep End! de bir göz atabilirsiniz.
Bu ZF ile bulunuyor; Ben Symfony veya CakePHP gibi diğer MVC Çerçeveleri şeyler aynı tür bulabilirsiniz varsayalım.