Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

php - Which page is which acording to model-view-controller?

EDIT: My below example is more of a post-redirect-get than a MVC

I am reading a lot about correct structure for my page, MVC pattern, frameworks etc. and yet I am confused which parts of my page best fit under the description of model, of view and which of controller. Now before you downvote I did a lot of research already to separate my logic and make my simple page, I just need a confirmation that I am doing it right, what to fix/separate, which page is what according to MVC and where would I link or include index.php? I am not asking much I hope just for a quick glance at my code.

I will provide 3 different pages I built in order they are initialized as an example:

html form, also displays processed data user starts here:

<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 that processes data and returns results:

<?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 page that has included html page from earlier and to which my data manipulating php script redirects to display results:

<?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';
?>

Which page is which acording to model-view-controller? Am I doing it right at all? Where would I link or include index.php?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Which page is which according to model-view-controller?

A page is a page. It isn't part of MVC, it is built using MVC.

The view is the class that (given some data) generates whatever is sent to the client (usually HTML).

The model is the class that operates on the data. It talks to your database or other data store.

The controller is the class that looks at the URL, decides which models and views are right for it and exchanges data between the submitted data, the models and the view.

Am I doing it right at all?

No

Where would I link or include index.php?

Your index page should just bootstrap your controller class.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...