我创建了自己的MVC框架,并对其他框架如何将属性从“控制器”发送到“视图”感到好奇。 Zend做了$this->view->name = 'value';
我的代码是:
file: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';
}
}
file: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');
}
}
file: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“view”文件中,我有:
<img src="<?php echo $this->p->banner ?>" />
这不起作用。我做了一些根本错误的事情还是我的逻辑错误?我此刻似乎正在转圈。非常感谢任何帮助。
答案 0 :(得分: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 ?>" />
希望这会有用......
答案 1 :(得分:0)
<img src="<?php echo $this->p->banner ?>" />
暗示你是一个班级的实例。你说它是一个外部文件,所以你可能会有类似的东西
$a = new services_hosting();
在这种情况下,而不是
$this->banner;
你会有
$a->banner;
答案 2 :(得分: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';
}
}