在查看PHTML文件访问性能已经在控制器中设置(Access properties in View

2019-10-17 10:16发布

我工作的一个小PHP的网站,是基于MVC。 我有一个前端控制器( 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框架做类似$this->view->title = 'My Title'; 在控制器,然后在这个视图类似<title><?php echo $view->title; ?></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>

Answer 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”问候你,你可以用我在原来的职位,这能更好地伸缩提到的方法。

注:下面是从我的回复复制到你原来的职位,并没有得到修复,以配合您的新代码的例子。 它表明你可以存储阵列()对多维数据并展示如何从您的视图访问它们。

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


文章来源: Access properties in View phtml file that have been set in the Controller