【问题标题】:Access properties in View phtml file that have been set in the Controller访问已在 Controller 中设置的 View phtml 文件中的属性
【发布时间】:2012-11-29 01:24:32
【问题描述】:

我正在开发一个基于 MVC 的小型 PHP 网站。我有一个前端控制器 (front.php),它加载控制器 (services.php),运行操作方法 (hostingAction()) 并包含 html (view.phtml)。 view.phtml ($this->renderContent()) 中有一个包含内部内容 (hosting.phtml) 的方法调用。

问题: 如何在hostingAction() 方法中设置属性(例如$title = 'My Title';),然后在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>

【问题讨论】:

    标签: php oop model-view-controller overloading


    【解决方案1】:

    您的Services 对象无权访问$view

    试试这些模组:

    front.php(带 setter)

    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 设为单例(根据您的 cmets):

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

    【讨论】:

    • 谢谢!这似乎可以解决问题。一个问题。除了将$view 传递给services 之外,我能否(在services 内)查看是否存在viewProperties 的实例并得到它?
    • 是的,您可以将 viewProperties 设为 Singleton - 我已经用一个示例更新了我的答案(未经测试,请原谅任何语法错误)。
    • 另外,请务必“接受”我的回答,因为它成功了 :) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2020-11-18
    • 1970-01-01
    • 2014-04-28
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多