0

問題があります。独自のMVCアプリを作成していますが、モデルとコントローラーの間で変数を渡す際に問題があるようです。コントローラからの出力は、いくつかのjson形式のデータを含む単一の変数であり、単純に見えます。

コントローラ

<?php 

class controllerLib 
{
     function __construct() 
     {
            $this->view = new view();
     }

     public function getModel($model) 
     {
            $modelName = $model."Model"; 
            $this->model=new $modelName();
     }
}

 class controller extends controllerLib
 {
     function __construct()
     {
            parent::__construct();
     } 

     public function addresses($arg = false) 
     {
            echo'Addresses '.$arg.'<br />';

            $this->view->render('addressesView');

            $this->view->showAddresses = $this->model->getAddresses(); 
     }
 }

 ?>

意見

 <?php 

 class view
 {
    function __construct()
    {
    }

    public function render($plik)
    {
        $render = new $plik();
    }
 }

 class addressesView extends view
 {
    public $showAddresses;

    function __construct()
    {
        parent::__construct();

        require 'view/head.php';

        $result = $this->showAddresses;


        require 'view/foot.php';
    }
 }


 ?>

ここで問題となるのは、$ this-> showAddressesがビューに渡されず、スタックしていることです。

4

1 に答える 1

0

コードにはさまざまな問題があります。

  1. render()は、関数の終了後に存在しないように、新しいビューをローカル変数に保存します

  2. $this->showAddressesコンストラクターの時点で値を持つことは期待できません。

ビューコンストラクターの外部のメソッドとしてrender()メソッドを実装する必要があります。

function __construct() 
{
    parent::__construct();

    require 'view/head.php';

    $result = $this->showAddresses; // (NULL) The object is not created yet


    require 'view/foot.php';
}

クラスを表示:

public function factory($plik) // Old render($splik) method
{
    return new $plik();
}

アドレスビュークラス:

function __construct() 
{
  parent::__construct();
}

function render()
{
    require 'view/head.php';

    $result = $this->showAddresses; // Object is created and the field has a value


    require 'view/foot.php';
}

コントローラクラス:

 $view = $this->view->factory('addressesView');
 $view->showAddresses = $this->model->getAddresses(); 
 $view->render();
于 2012-09-11T07:54:12.350 に答える