编辑:我的下面的例子更多的是重定向后获取而不是MVC
我正在阅读关于我的页面,MVC模式,框架等的正确结构的很多内容,但我很困惑我的页面的哪些部分最适合模型,视图和控制器的描述。在你投票之前我已经做了很多研究来分离我的逻辑并制作我的简单页面,我只需要确认我做得对,修复/分离什么,根据MVC哪个页面是什么我会在哪里链接还是包含index.php?我不是很想问我希望只是快速浏览一下我的代码。
我将提供3个不同的页面,以便将它们初始化为例:
html表单,还显示用户从此处开始处理的数据:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Vaški gozd</title>
<link href="../html/css/base.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php if(!$_POST and $poskodbe != '0') { ?>
<p>Presenetil<?php text($spol); ?> te je <?php text($monster); ?> !</p>
<form action='../php/gozd.php' method='post'>
<input type='submit' name='action' value='Napadi' /> ali
<input type='submit' name='action' value='Pobegni' />
<input type='hidden' name='monster' value= '<?php text($monster); ?>' />
</form>
<?php }
else if ($poskodbe == '0'){text($moznost); ?>
<p><a href='../php/start.php'>Odpravi se proti domu</a></p>
<?php }
else { ?>
<ul><?php foreach ($combat as $turns => $i) { ?>
<li> <p><strong><?php text($i['napadalec']); ?></strong>
<?php text(' napade '); ?><strong><?php text($i['branilec']); ?></strong>
<?php text(' in mu napravi poškodbe za ') ?><strong><?php text($i['damage']); ?></strong>
<?php text(' točk zdravja '); } ?> </p></li>
</ul>
<?php if(isset ($zmaga)) { ?>
<p>Pregnal si <strong><?php text($monster_ime); ?></strong>!
V naglici je za seboj pustil <strong><?php text($cekini); ?></strong> cekinov, ki jih seveda pobereš.</p>
<p><a href='../php/gozd.php'>Raziskuj dalje</a></p>
<?php } ?>
<?php if(isset ($zguba)) { ?>
<p>Podlegel si poškodbam <strong><?php text($monster_ime); ?></strong>.</p>
<?php } ?>
<p><a href='../php/start.php'>Odpravi se proti domu</a></p>
<?php } ?>
</body>
</html>
处理数据并返回结果的php:
<?php
session_start();
include 'config.php';
include 'stats.php';
$igralec_ime = $_SESSION['username'];
$_SESSION['poskodbe'] = ($poskodbe = prikazi_stat('curhp', $igralec_ime));
if ($poskodbe == '0') {$_SESSION['moznost'] = ($moznost = 'Tvoje zdravje je resno ogroženo, vrni se domov!');}
else {
if ($_POST) {
if($_POST['action'] == 'Napadi') {
$igralec = array (
'ime' => $igralec_ime,
'napad' => prikazi_stat('ofe',$igralec_ime),
'obramba' => prikazi_stat('def',$igralec_ime),
'curhp' => prikazi_stat('curhp',$igralec_ime)
);
$monster_ime = $_POST['monster'];
$monster = array (
'ime' => $monster_ime,
'napad' => prikazi_monster_stat('ofe',$monster_ime),
'obramba' => prikazi_monster_stat('def',$monster_ime),
'curhp' => prikazi_monster_stat('maxhp',$monster_ime)
);
$combat = array();
$turns = 0;
while($igralec['curhp'] > 0 && $monster['curhp'] > 0) {
if($turns % 2 != 0) {
$napadalec = &$monster;
$branilec = &$igralec; }
else {
$napadalec = &$igralec;
$branilec = &$monster; }
$damage = 0;
if($napadalec['napad'] > $branilec['obramba']) {
$damage = $napadalec['napad'] - $branilec['obramba']; }
$branilec['curhp'] -= $damage;
$combat[$turns] = array(
'napadalec' => $napadalec['ime'],
'branilec' => $branilec['ime'],
'damage' => $damage
);
$turns++; }
update_stat('curhp',$igralec_ime,$igralec['curhp']);
if($igralec['curhp'] > 0) {
update_stat('cek',$igralec_ime,prikazi_stat('cek',$igralec_ime)+ prikazi_monster_stat('cek',$monster_ime));
$zmaga = 1;
$cekini = prikazi_monster_stat('cek',$monster_ime); }
else {
if ($igralec['curhp'] <0) {update_stat('curhp', $igralec_ime, '0'); }
$zguba = 1; } }
else {
header('Location:../php/start.php');
exit;
}
}
else {
$query = sprintf("SELECT ime, spol FROM monsters ORDER BY RAND() LIMIT 1");
$result = mysql_query($query);
list($monster, $spol) = mysql_fetch_row($result);
}
}
$_SESSION['moznost'] = $moznost;
$_SESSION['monster'] = $monster;
$_SESSION['spol'] = $spol;
$_SESSION['poskodbe'] = $poskodbe;
$_SESSION['combat'] = $combat;
$_SESSION['turns'] = $turns;
$_SESSION['zmaga'] = $zmaga;
$_SESSION['zguba'] = $zguba;
$_SESSION['monster_ime'] = $monster_ime;
$_SESSION['cekini'] = $cekini;
$_SESSION['post'] = $_POST;
header('Location:../php/gozd_kontroler.php',true,303);
exit;
?>
php页面,其中包含了之前的html页面,我的数据操作php脚本重定向到显示结果:
<?php
session_start();
include 'razno.php';
$monster = $_SESSION['monster'];
$spol = $_SESSION['spol'];
$poskodbe = $_SESSION['poskodbe'];
$moznost = $_SESSION['moznost'];
$combat = $_SESSION['combat'];
$turns = $_SESSION['turns'];
$zmaga = $_SESSION['zmaga'];
$zguba = $_SESSION['zguba'];
$monster_ime = $_SESSION['monster_ime'];
$cekini = $_SESSION['cekini'];
$_POST = $_SESSION['post'];
include '../html/gozd.html';
?>
哪个页面是根据模型 - 视图 - 控制器?我做得对吗?我将在哪里链接或包含index.php?
答案 0 :(得分:2)
哪个页面是根据模型 - 视图 - 控制器?
页面是一个页面。它不是MVC的 part ,它是使用MVC构建的。
视图是(给定一些数据)生成发送到客户端的任何内容的类(通常是HTML)。
模型是对数据进行操作的类。它与您的数据库或其他数据存储进行对话。
控制器是查看URL的类,决定哪些模型和视图适合它,并在提交的数据,模型和视图之间交换数据。
我是否正确行事?
没有
我将在哪里链接或包含index.php?
您的索引页面应该只是引导您的控制器类。