(root)/trunk/Controller.php - Rev 46
Rev 30 |
Rev 52 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
1
<?php
require_once __DIR__
. '/Application.php';
require_once __DIR__
. '/View.php';
/* lcfirst() is unavailable before PHP 5.3 */
if (false === function_exists('lcfirst'))
{
/**
* Make a string's first character lowercase
*
* @param string $str The input string.
* @return string The resulting string.
* @link http://www.php.net/manual/en/function.lcfirst.php
*/
function lcfirst
($str)
{
return strtolower(substr($str, 0, 1)) . substr($str, 1);
}
}
/**
* A general controller that can handle views according to
* the MVC pattern
*
* @author tlahn
*/
abstract class Controller
{
/**
* Default action of the controller
* @var string
*/
protected $_defaultAction = 'index';
/**
* If an array, maps an action name to an action method of the controller.
*
* Fallback for IE 7 where the content of a <code>button</code> element
* is submitted as value.
*
* @var array
*/
protected $_actionMap = null;
/**
* The {@link View} used by this controller
* @var View
*/
protected $_view = null;
/**
* Constructs a controller, initializes the related view,
* and calls the controller's URI-indicated action method.
*
* @param string $viewClass
* View class. The default is <code>'View'</code>.
* @param string $template
* Resource path of the template for the view. The default
* is to <code>null</code> (no template).
*
* @see View::__construct()
*/
protected function __construct
($viewClass = 'View', $template = null)
{
$this->_view
= new $viewClass($template);
Application
::getInstance()->setCurrentController($this);
$action = Application
::getParam('action', $_REQUEST);
/* NOTE: No `==='; treat empty action like no specific action */
if ($action == null)
{
$action = $this->_defaultAction
;
}
$actionMethod = lcfirst
($action) . 'Action';
/* Fallback for IE 7 where the content of a `button' element is submitted as value */
if (!method_exists($this, $actionMethod))
{
if (is_array($this->_actionMap
) && array_key_exists($action, $this->_actionMap
))
{
$actionMethod = $this->_actionMap
[$action];
}
}
$this->$actionMethod();
}
/**
* Assigns a value to a template variable (after this,
* <var>$value</var> is available through
* <code>$this-><var>$name</var></code> in the view's template).
* <code>Controller</code>s should call this method instead of
* {@link View::assign()}.
*
* @param string $name
* Variable name
* @param mixed $value
* Variable value
* @param bool $encodeHTML
* If <code>true</code>, replace all potentially conflicting
* characters in <var>$value</var> with their HTML entity
* references. The default is <code>false</code>.
* @return mixed The assigned value (after possible HTML encoding)
* @see View::encodeHTML()
*/
protected function assign
($name, $value, $encodeHTML = false)
{
return $this->_view
->assign($name, $value, $encodeHTML);
}
/**
* Renders the {@link View} associated with this controller
* by including the <code>View</code>'s template.
* <code>Controller</code>s should call this method instead of
* <code>View::render()</code>.
*
* @param string $template
* Optional alternative template resource path.
* If not provided, the default template (the
* <code>View</code>'s <code>$template</code> property)
* will be used.
*/
public function render
($template = null, $content = null)
{
$this->_view
->render($template, $content);
}
}
?>