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.