-2

I've sort of created my own framework to develop my personal portfolio website. I've based it (very) loosely on Zend Framework, which I am fairly familiar with. I've managed to get things working apart from setting variables within the controller which can then be used in the .phtml files. This is what I have at the moment (e.g. url here is www.mydomain.com/services/hosting):

front.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;
    }
}

viewProperties.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]);
    }
}

services.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';
    }
}

in hosting.phtml I have:

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

How can I set properties (not just for a banner) in the controller and then retrieve them in the view? Help/guidance would be really appreciated.

4

1 に答える 1

1

Since you didn't specify exactly what problem you are experiencing, I will have to guess from the code.

I am guessing that you are not currently able to set/retrieve the banner properties?

If so, try this:

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

This is a simple fix to achieve a working model, but not exactly what you were aiming for.

To create a dynamic variable that would allow you to access multiple levels via '->' you would probably need to create a ViewElement type that wrapped the resulting assignment and supplied the _* methods.

In your example, banner has not actually been created (i.e. _set() is never called) before it is retrieved via _get() - so your code needs to auto-create a value when _get() is called on a non-existing value.

I also recommend that your ViewProperties extend ViewElement to reduce duplicated code.

Here's something I whipped up (the magic is in __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}'");

[edit] Fat-fingered and submitted before complete.

[edit] Expanding answer with possible solution.

于 2012-11-27T00:01:53.297 に答える