我工作的一个小PHP的网站,是基于MVC。 我有一个前端控制器( front.php
),该加载控制器( services.php
),运行动作方法( hostingAction()
并且包括在HTML( view.phtml
)。 有在view.phtml一个方法调用( $this->renderContent()
其包括内部内容( hosting.phtml
)。
问题:如何可以设置属性(例如$title = 'My Title';
在hostingAction()
方法,然后在view.phtml做<title><?php echo $title ?></title>
Zend框架做类似$this->view->title = 'My Title';
在控制器,然后在这个视图类似<title><?php echo $view->title; ?></title>
<title><?php echo $view->title; ?></title>
目前,我超载的属性。 我管理的设置在控制器动作的属性,但不能访问他们在我的观点。 我在做什么错在这里?
示例代码:
front.php
class front {
private $view;
function __construct() {
$this->view = new viewProperties();
$this->constructController();
include('application/views/view.phtml');
}
private function constructController() {
$c = new services();
$this->doAction($c);
}
public function renderContent() {
include('application/views/services/hosting.php');
}
private function doAction($c) {
$c->hostingAction();
}
}
services.php
class services {
public function hostingAction() {
$this->view->page_title = 'Services - Hosting';
$this->view->banner_src = '/assets/images/banners/home_02.jpg';
$this->view->banner_title = 'reload';
}
}
viewProperties.php
class viewProperties {
private $data = array ();
public function __set($name, $value) {
$this->data[$name] = $value;
}
public function __get($name) {
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
}
}
view.phtml
<html>
<head>
<title><?php echo $this->view->page_title; ?></title>
</head>
<body>
<?php $this->renderContent() ?>
</body>
</html>
hosting.phtml
<div id="banner">
<img src="<?php echo $this->view->banner_src ?>" alt="<?php echo $this->view->banner_title ?>" />
</div>