Howdy, I’m writing a very simple little PHP script to manage messages via a front-end form.
I’ve written a “controller” file which checks the source of the request GET|POST and decides how to proceed based on that.
If there is a POST, I validate the form that its my form, validate the form itself, filter values etc and finally prepare variables to be passed back to the “view”.
At the moment, I’m using this technique:
class View
{
protected $_vars = array();
public function set($name, $value)
{
$this->_vars[$name] = $value;
}
public function render($view)
{
$file = sprintf('%s.php', $view);
if (!file_exists($file) && !is_readable($file)) {
return false;
}
if (is_array($this->_vars) && !empty($this->_vars)) {
extract($vars);
}
require_once $file;
exit;
}
}
$view = new View;
$view->set('greeting', 'Hello World');
$view->render('index');
I’ve looked at other frameworks like Zend to see how they do it, I think they use some kind of stream context technique that I’ve never seen before.
I dont need all the bulk of a framework for my little script but I would like to build it as best I can. So I was wondering if anyone could suggest a good technique to include view files, I would also like to give my view access to $this if possible.
Thanks!
Tyler