1

我正在开发一个基于 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>.

目前我正在重载属性。我正在设法在控制器操作中设置属性,但无法在我的视图中访问它们。我在这里做错了什么?

示例代码:

前台.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();
    }
}

服务.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';
    }
}

视图属性.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];
        }
    }
}

视图.phtml

<html>
    <head>
        <title><?php echo $this->view->page_title; ?></title>
    </head>
    <body>

    <?php $this->renderContent() ?>

    </body>
</html>

托管.phtml

<div id="banner">
            <img src="<?php echo $this->view->banner_src ?>" alt="<?php echo $this->view->banner_title ?>" />
</div>
4

1 回答 1

1

您的Services对象无权访问$view.

试试这些模组:

front.php(带有设置器)

class front {

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

services.php(带有设置器)

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',你可以使用我在你原来的帖子中提到的方法,它可以更好地扩展。

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

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'] ?>" />
于 2012-11-29T07:41:10.153 回答