1

我正在使用 MVC 模式,在我的网站中实现一个部分,以购买公开的文章。然而,我有一个很大的问题来实现这部分,特别是在我的控制器之间交换数据,以及在所有视图中使用这些数据。

现在,我尝试了两种解决方案来在控制器之间传递这些数据:

  • 第一个是在我的控制器代码的顶部创建我的模型类的一些对象,可以从所有 switch 分支(以及我的控制器的所有部分)修改它。我不明白为什么,从一个视图传递到另一个视图,总是通过同一个控制器,我的对象被重置并变为空。
  • 第二种,一个不太有用的解决方案是使用一个大数组,从一个视图传递到另一个视图(从同一个控制器的 switch 分支到另一个分支),通过一个表单,或者,但是,超全局变量。

我向您展示视图和代码:

第一张图是登录视图;用户没有登录,已经指定了他想要接收对象的格式并且必须登录。

http://i60.tinypic.com/2nq9rfd.jpg

第二张图片是该用户登录后的视图。所有用户和对象数据都是通过查询获得的,但我需要记住用户希望接收图片的格式!

格式……选择的付款方式,等等。

http://i61.tinypic.com/2v2xkt3.jpg

编码..

我的 MVC 模式包含一个 index.php(访问我的站点的唯一点),它实例化一个填充我的 master.php 的类“X”-Controller 的对象。

master.php 是在我的控制器方便的情况下的动态页面填充,它实例化所有变量和对象变量以组成请求的视图。

特别是,这是购买部分的代码:

控制器

(完整(相对)url = /index.php?page=works&subpage=payment&imageID=00000001)

class WorksController
{
    public static $counter = 0;

    public function __construct(&$request, &$session)
    {
        self::$counter++;
        $this -> handle_input($request, $session);

    }

    private function handle_input(&$request, &$session)
    {
        //It create a new Album (class RaccoltaImmagini), that include all pictures (class Immagine) that have to be sell
        if(!isset($raccoltaImmagini))
        {

            $raccoltaImmagini = PicturesCollectionConstructor::buildNewCollection("AllPictures"); 

        }

        if(!isset($riepilogoOrdine))
        {
            //IT creates a new object (class Ordine) that include all infos to complete and to manage the transaction
            $riepilogoOrdine = new Ordine(null, null, null, null, null, null, null, null, null); 

        }

        if(!isset($userOBJ))
        {
            //It creates a new object that rappresent the user logged in and all the data associated (like credential, name, adress, credit card..)
            $userOBJ = new AuthenticatedUser(null, null, null, null, null, null,null, null, null, null, null, null, null, null);
        }

        switch($request["subpage"]) 
        {
            default: include("PHP/view/content-not-found.php");
                     break;

            case "payment": //This part of the Controller manage the view to select the payment method:             
                            //if the user is not logged in, the view charge the secondary form for the login
                            //else the view show a short recap about the options already selected, and the method of payments registered to choose


                            if(isset($request["imageID"]))  //Step 1 - The authenticated user has to choose the payment's method
                            {
                                if(isset($_SESSION["loggedIn"]) && $_SESSION[ "loggedIn"]) 
                                {
                                    if( isset($session["username"]) && isset($session["password"]))//User authenticated
                                    {   
                                        //It show which formats have been selected (it store an array of booleans, where array[$i] is true if the format is requested)                                          
                  $formati = $riepilogoOrdine -> getAllReservedFormats();


                                        //It initialize the object that represent the authenticated User
                                        $userOBJ = UserConstructor::buildUser($session["username"], $session["password"]); 

                                        //It initialize the object that represent the pictures that has to be sell
                                        $describedOBJ = $raccoltaImmagini -> getImagesByID($request["imageID"]);

                                        //- It update, newly, the information about the order:

                                        //-- It associates the user-id to ther order
                                        $riepilogoOrdine -> setUserID($userOBJ -> getID()); 

                                        //-- it associates the image-id to ther order
                                        $riepilogoOrdine -> setArticleID($describedOBJ -> getID());

                                        //-- it associates the partial import to ther order
                                        $riepilogoOrdine -> setTotalImport($describedOBJ -> getPrice());



                                        $style = "PHP/view/TransactionsStyle.php";
                                        $header = "PHP/view/Header.php";
                                        $loginFormContent = "PHP/view/loggedUserMenu.php";
                                        $slideshow=null;
                                        $works = "PHP/view/Works.php";
                                        $preview250="PHP/view/preview250.php";
                                        $SecondaryLoginForm = null;
                                        $summaryPayments = "PHP/view/summaryPayments.php";
                                        $ToPay = "PHP/view/ToPay.php";
                                        $footer="PHP/view/footer.php"; 


                                        include("master.php");
                                    }
                                }
                                else //Step 0 - The user is not still logged in
                                {

                                    //It initialize the object that represent the pictures that has to be sell 
                                    $describedOBJ = $raccoltaImmagini -> getImagesByID($request["imageID"]);


                                    //- It update the information about the order:

                                    //-- it associates the partial import to ther order
                                    $riepilogoOrdine -> setTotalImport($describedOBJ -> getPrice());

                                    //-- It associates to the order the formats select (obtained trough a previous submit) 
                                    if(isset($_REQUEST["formats"]))
                                    {
                                        $formats = array();
                                        $formats = $_REQUEST["formats"];


                                        if(isset($formats[0])) //online formats
                                        {
                                            $riepilogoOrdine -> setOnlineFormat(True); 
                                        }

                                        if(isset($formats[1])) //gallery1 format
                                        {
                                            $riepilogoOrdine -> setGallery1Format(True); 
                                        }

                                        if(isset($formats[2])) //gallery2 format
                                        {
                                            $riepilogoOrdine -> setGallery2Format(True);
                                        }

                                    }


                                    //It show which formats have been selected (it store an array of booleans, where array[$i] is true if the format is requested)                                          
                                    $formati = $riepilogoOrdine -> getAllReservedFormats();

                                    $style = "PHP/view/TransactionsStyle.php";
                                    $header = "PHP/view/Header.php";
                                    $loginFormContent = "PHP/view/LoginFormContent.php"; 
                                    $works = "PHP/view/Works.php";
                                    $preview250="PHP/view/preview250.php";

                                    $SecondaryLoginForm="PHP/view/SecondaryLoginFormContent.php";
                                    $ToPay = "PHP/view/ToPay.php";



                                    $footer="PHP/view/footer.php"; 
                                    include("master.php");
                                }
                            }
                            else
                            {
                                include("PHP/view/content-not-found.php");
                            }

                            break;
 [...] //other code omitted
} //swith closure

风景

<?php $IMGid = $describedOBJ -> getID(); ?>
<!-- It contains a recap for every step of transaction -->
<div id="Riepilogo">  

<!-- It stores the information about the transaction -->
<div id="Transazione">
    <?php if(isset ($SecondaryLoginForm)) include($SecondaryLoginForm); ?> <!-- Step 0: User is not logged in -->

    <?php if(isset ($summaryPayments)) include($summaryPayments); ?> <!-- Step 1: User logged in and selection of payment's method -->

    <?php if(isset ($shipments)) include($shipments); ?> <!-- Step 2: selection of the shipment's method -->

</div>

<!-- It contains a recap about the pictures to buy -->
<div id="RiepilogoVisuale">

    <!-- close the window -->
    <button name="chiudi" type="button" id="closingButton" onclick="javascript:chiudi();">Chiudi</button>

    <!-- It contains details about the pictures -->
    <article id="descrizioneImmagine">
        <header id="TitleAuthor">
            <h1><?php echo($describedOBJ -> getTitolo());?>   by  </h1>
            <h2><?php echo($describedOBJ -> getAutore());?></h2>
        </header>

        <section id="previewImmagine">
            <img src="<?php echo($describedOBJ -> getPreview850());?>" alt="<?php echo($describedOBJ->getDescrizione());?>"> 
        </section>

        <!-- information about price, formats etc -->
        <section id="riepilogoAcquisto">
            <h1>Riepilogo acquisto:</h1>

            <div id="formatiIMG">
                <h2>Formati Selezionati</h2>
                <ul>

                    <?php
                        if(isset($formati["online"]) && $formati["online"]) 
                        {?>
                            <li><?php  echo("Formato online (High Res.)")?></li>
                    <?php }
                        if(isset($formati["gallery1"]) && $formati["gallery1"]) 
                        {?>
                            <li><?php echo("Formato galleria piccolo(90x160 cm)")?></li>
                    <?php }
                        if(isset($formati["gallery2"]) && $formati["gallery2"]) 
                        {?>
                            <li><?php echo("Formato galleria grande (100x200 cm)")?></li>
                    <?php }?>
                </ul>
            </div>

            <div id="AdditionalInfo">
                <section id="prezzo">
                    <h2>Prezzo:</h2>
                    <ul>
                        <li><?php echo($riepilogoOrdine -> getTotalImport())?>€&lt;/li>
                    </ul>
                </section>

                <section id="spedizione">
                    <h2>Metodo di spedizione:</h2>
                    <ul>
                        <li>Corriere Espresso</li>
                    </ul>
                </section>

            </div>

        </section>
    </article>
</div>

希望大家能帮帮我,谢谢大家的耐心

编辑:我添加了我的 index.php 的代码,它调用了所有的控制器

  <?php 
  //include directives for all the controllers
  //code omitted


Dispatcher::dispatch($_REQUEST);

class Dispatcher
{


public static function dispatch(&$REQUEST)
{

    session_start();

    if(isset($REQUEST["page"]))
    {
        switch($REQUEST["page"]) 
        {

            default: 
                    include("PHP/view/content-not-found.php");
                    break;

            //code from other controllers omitted
            case "works": 
                    new WorksController($REQUEST, $_SESSION);
                    break;

        }

    }

}



}

  ?>
4

1 回答 1

0

我不确定是否可以成为完美的解决方案,但我认为 $_SESSION 数组对用户的作用。我必须在控制器之间存储和交换的数据是仅与用户相关的数据,并且需要继续到下一个视图...所以我使用 $_SESSION["otherData"] 来存储我的摘要数组的内容:

 //It stores the data on the server, first than write them to the Database
 $_SESSION["otherData"] = $riepilogoOrdine; 

 //where riepilogoOrdine track     
 //the information, obtained from the user, about the order
于 2015-03-20T08:54:10.810 回答