问题
I am working on a small PHP website which is based on MVC. I have a front controller (front.php
) which loads the controller (services.php
), runs the action method (hostingAction()
) and includes the html (view.phtml
). There is a method call in view.phtml ($this->renderContent()
) that includes the inner content (hosting.phtml
).
QUESTION:
How can I set properties (e.g. $title = 'My Title';
) in the hostingAction()
method and then in view.phtml do <title><?php echo $title ?></title>
?
Zend Framework does something like $this->view->title = 'My Title';
in the controller and then in the view does something like <title><?php echo $view->title; ?></title>
.
Currently I am overloading the properties. I am managing to set the properties in the controller action but failing to access them in my view. What am I doing wrong here?
SAMPLE CODE:
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>
回答1:
Your Services
object does not have access to $view
.
Try these mods:
front.php (with setter)
class front {
private function constructController() {
$c = new services();
$c->setView($this->view);
$this->doAction($c);
}
}
services.php (with setter)
class services {
private $view;
public function setView(viewProperties $view) {
$this->view = $view;
}
public function hostingAction() {
$this->view->page_title = 'Services - Hosting';
$this->view->banner_src = '/assets/images/banners/home_02.jpg';
$this->view->banner_title = 'reload';
}
}
Using a Singleton
Also, you could make viewProperties a singleton (per your comments):
viewProperties (with singleton)
class viewProperties {
private $instance = null;
private function __construct() {}
public static function getInstance() {
if (null === $this->instance) {
$this->instance = new self();
}
return $this->view;
}
}
front (with singleton)
class front {
private $view;
function __construct() {
$this->view = viewProperties::getInstance();
$this->constructController();
include('application/views/view.phtml');
}
}
services.php (with singleton)
class services {
private $view;
function __construct() {
$view = viewProperties::getInstance();
}
public function hostingAction() {
$this->view->page_title = 'Services - Hosting';
$this->view->banner_src = '/assets/images/banners/home_02.jpg';
$this->view->banner_title = 'reload';
}
}
Using Multi-dimensional Variables
Finally, in regards to you using 'banner_src' and 'banner_title', You can use the method I mentioned in your original post, which scales better.
NOTE The example below was copied from my reply to your original post, and has not been fixed to match your new code. It is showing that you can store arrays() for multidimensional data and shows how to access them from your view.
class services extends controller {
public function indexAction() {
$this->view->banner = array
(
'src' => '/path/to/images/banners/home_02.jpg',
'alt' => 'banner title'
);
}
public function hostingAction() {
$this->view->banner = array
(
'src' => '/path/to/images/banners/home_02.jpg',
'alt' => 'banner title'
);
}
}
<img src="<?php echo $this->view->banner['src'] ?>" alt="<?php echo $this->view->banner['title'] ?>" />
来源:https://stackoverflow.com/questions/13617336/access-properties-in-view-phtml-file-that-have-been-set-in-the-controller