1

私はMVCに基づく小さなPHPWebサイトに取り組んでいます。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>
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 に答える