在Controller中已设置的View phtml文件中的访问属性 [英] Access properties in View phtml file that have been set in the Controller

查看:71
本文介绍了在Controller中已设置的View phtml文件中的访问属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

问题: 如何在hostingAction()方法中设置属性(例如$title = 'My Title';),然后在view.phtml中设置<title><?php echo $title ?></title>?

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在控制器中执行类似于$this->view->title = 'My Title';的操作,然后在视图中执行诸如<title><?php echo $view->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?

示例代码:

front.php

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

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

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

view.phtml

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

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

    </body>
</html>

hosting.phtml

hosting.phtml

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

推荐答案

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

尝试这些mod:

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设置为单个(根据您的评论):

Using a Singleton

Also, you could make viewProperties a singleton (per your comments):

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",您可以使用我在原始帖子中提到的方法,这种方法可以更好地扩展.

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.

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

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

这篇关于在Controller中已设置的View phtml文件中的访问属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆