-2

我已经创建了自己的框架来开发我的个人投资组合网站。我将它(非常)松散地基于我相当熟悉的 Zend Framework。除了在控制器中设置变量之外,我已经设法让事情正常工作,然后可以在 .phtml 文件中使用这些变量。这就是我目前所拥有的(例如,这里的 url 是 www.mydomain.com/services/hosting):

前台.php

class front {
    private $sControllerName = 'home';
    private $sActionName = 'index';

    function __construct() {
        $this->view = new viewProperties();
        $aUriParts = $this->getUriParts();
        if (!empty($aUriParts[0])) {
            $this->sControllerName = $aUriParts[0];   
        }
        if (!empty($aUriParts[1])) {
            $this->sActionName = $aUriParts[1];   
        }
        $this->constructController();
        include('path/to/views/view.phtml');
    }

    private function constructController() {
        $sContFile = 'controllers/'.$this->sControllerName.'.php';
        if (@include($sContFile)) {
            $c = new $this->sControllerName();
            // Action method, if exists
            $this->doAction($c);
        }
    }

    public function getElement($name) {
        include('public_html/elements/'.$name);
    }

    public function renderContent() {
        $sViewFile = 'path/to/views/'.$this->sControllerName;
        if ($this->sActionName) {
            $sViewFile .= '/'.$this->sActionName.'.phtml';
        } else {
            $sViewFile .= '.php';
        }
        if (!@include($sViewFile)) {
            $this->render404();
        }
    }

    private function doAction($c) {
        $sActionFunc = str_replace('-', '_', $this->sActionName.'Action');
        if (method_exists($c,$sActionFunc)) {
            $c->$sActionFunc();
        }
    }

    private function render404() {
        include('path/to/views/404.phtml');
    }

    private function getUriParts() {
        $sUri = $_SERVER['REQUEST_URI'];
        $sUri = trim($sUri, "/");
        $sUri = str_replace("-", "_", $sUri);
        $aUriParts = explode("/", $sUri);
        return $aUriParts;
    }
}

视图属性.php

class viewProperties {
    static $_instance = null;
    private $data = array();

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function __set($name, $value) {
        echo "Setting '$name' to '$value'\n";
        $this->data[$name] = $value;
    }

    public function __get($name) {
        echo "Getting '$name'\n";
        if (array_key_exists($name, $this->data)) {
            return $this->data[$name];
        }
    }

    public function __isset($name) {
        echo "Is '$name' set?\n";
        return isset($this->data[$name]);
    }

    public function __unset($name) {
        echo "Unsetting '$name'\n";
        unset($this->data[$name]);
    }
}

服务.php

class services extends controller {
    public function indexAction() {
        $this->view->banner->src = '/path/to/images/banners/home_02.jpg';
        $this->view->banner->alt = 'banner title';
    }

    public function hostingAction() {
        $this->view->banner->src = '/path/to/images/banners/home_02.jpg';
        $this->view->banner->alt = 'banner title';
    }
}

在hosting.phtml我有:

<img src="<?php echo $this->view->banner->src ?>" alt="<?php echo $this->view->banner->title ?>" />

如何在控制器中设置属性(不仅仅是横幅),然后在视图中检索它们?帮助/指导将不胜感激。

4

1 回答 1

1

由于您没有具体说明您遇到了什么问题,我将不得不从代码中猜测。

我猜您目前无法设置/检索横幅属性?

如果是这样,试试这个:

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

这是实现工作模型的简单修复,但不完全是您的目标。

要创建一个允许您通过“->”访问多个级别的动态变量,您可能需要创建一个ViewElement类型来包装结果赋值并提供 _* 方法。

在您的示例中,在通过检索它之前banner尚未实际创建(即_set()从未调用)_get()- 因此您的代码需要_get()在对不存在的值调用时自动创建一个值。

我还建议您的 ViewProperties 扩展 ViewElement 以减少重复代码。

这是我整理的东西(魔法在__get()):

class viewProperties extends ViewElement {
    static $_instance = null;
    private $data = array ();
    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self ();
        }

        return self::$_instance;
    }
}
class ViewElement {
    private $data = array ();
    public function __set($name, $value) {
        echo "Setting '$name' to '$value'\n";
        $this->data [$name] = $value;
    }
    public function __get($name) {
        echo "Getting '$name'\n";
        if (! array_key_exists ( $name, $this->data )) {
            $this->data [$name] = new ViewElement();
        }
        return $this->data [$name];
    }
    public function __isset($name) {
        echo "Is '$name' set?\n";
        return isset ( $this->data [$name] );
    }
    public function __unset($name) {
        echo "Unsetting '$name'\n";
        unset ( $this->data [$name] );
    }
}

$view = viewProperties::getInstance();
$view->boo = "hoo";
$view->foo->bar = "baz";
print ("boo = '{$view->boo}', foo->bar='{$view->foo->bar}'");

[编辑] 胖手指并在完成之前提交。

[编辑] 用可能的解决方案扩展答案。

于 2012-11-27T00:01:53.297 回答