编辑:我下面的例子更像是一个后重定向获取而不是一个 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?