0

我已经创建了自己的 MVC 框架,并且很好奇其他框架如何将属性从“控制器”发送到“视图”。Zend 做了一些事情,$this->view->name = 'value';我的代码是:

文件:services_hosting.php

class services_hosting extends controller {
    function __construct($sMvcName) {
        parent::__construct($sMvcName);

        $this->setViewSettings();
    }

    public function setViewSettings() {        
        $p = new property;
        $p->banner = '/path/to/banners/home.jpg';
    }
}

文件:controller.php

class controller  {
    public $sMvcName = "home";

    function __construct($sMvcName) {
        if ($sMvcName) {
            $this->sMvcName = $sMvcName;
        }

        include('path/to/views/view.phtml');
    }

    public function renderContent() {
        include('path/to/views/'.$this->sMvcName.'.phtml');
    }
}

文件:property.php

class property {

    private $data = array();
    protected static $_instance = null;

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }

    public function __isset($name) {
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        unset($this->data[$name]);
    }

}

在我的 services_hosting.phtml “视图”文件中,我有:

<img src="<?php echo $this->p->banner ?>" />

这只是行不通。我是在做一些根本错误的事情还是我的逻辑不正确?我现在似乎在兜圈子。任何帮助将不胜感激。

4

3 回答 3

0

我的前控制器看起来像这样

class front {

    private $sMvcName = 'home';

    function __construct() {
        $aUriParts = $this->getUriParts();

        if ($aUriParts[0]) {
            $this->sMvcName = implode("_", $aUriParts);   
        }

        $this->constructController();
    }

    private function constructController() {
        $this->view = new property();
        $sControllerName = $this->sMvcName;
        new $sControllerName($this->sMvcName);
    }

    private function getUriParts() {
        $sUri = $_SERVER['REQUEST_URI'];
        $sUri = trim($sUri, "/");
        $sUri = str_replace("-", "_", $sUri);
        $aUriParts = explode("/", $sUri);
        return $aUriParts;
    }

}

services_hosting.php 现在看起来像这样

class services_hosting extends controller {

    function __construct($sMvcName) {
        $this->setViewSettings();

        parent::__construct($sMvcName);
    }

    public function setViewSettings() {
        $this->view->banner = '/assets/images/banners/home_02.jpg';
    }

}
于 2012-11-09T21:25:14.047 回答
0

你能检查这是否有效吗?

在您的 controller.php 中,您创建视图对象

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

现在在我们的服务托管控制器中

class services_hosting extends controller {
function __construct($sMvcName) {
    parent::__construct($sMvcName);

$this->view->banner = '/path/to/banners/home.jpg';
}

}

现在应该可以在您的视图文件中访问横幅变量。

<img src="<?php echo $this->banner ?>" />

希望这会奏效...

于 2012-11-09T06:25:30.453 回答
0
    <img src="<?php echo $this->p->banner ?>" />

暗示你在一个类的实例中。你说它是一个外部文件,所以你可能会有类似的东西

  $a = new services_hosting(); 

在这种情况下,而不是

 $this->banner;

你将会拥有

$a->banner;
于 2012-11-09T10:47:40.280 回答