在View phtml文件中访问已在Controller中设置的属性

时间:2012-11-29 01:24:32

标签: php oop model-view-controller overloading

我正在开发一个基于MVC的小型PHP网站。我有一个前端控制器(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 Framework在控制器中执行类似$this->view->title = 'My 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>

1 个答案:

答案 0 :(得分:1)

您的Services对象无法访问$view

试试这些mod:

front.php (使用setter)

class front {

    private function constructController() {
        $c = new services();
        $c->setView($this->view);
        $this->doAction($c);
    }
}

services.php (使用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';
    }
}

使用单身人士

此外,您可以使viewProperties成为单身人士(根据您的评论):

viewProperties (带单身)

class viewProperties {

    private $instance = null;

    private function __construct() {}

    public static function getInstance() {
        if (null === $this->instance) {
            $this->instance = new self();
        }
        return $this->view;
    }
}

前面(带单身)

class front {

    private $view;

    function __construct() {

        $this->view = viewProperties::getInstance();
        $this->constructController();
        include('application/views/view.phtml');
    }
}

services.php (使用单身人士)

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';
    }
}

使用多维变量

最后,关于你使用'banner_src'和'banner_title',你可以使用我在原帖中提到的方法,它可以更好地扩展。

注意以下示例已从我的回复复制到您的原始帖子,并且尚未修复以匹配您的新代码。它表明您可以存储多维数据的数组(),并显示如何从您的视图中访问它们。

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'] ?>" />