A really simple PHP Controller class

I have written a reaaaally simple Controller class to my hobby project, Wigbi. Even though it is SO basic, it will help immensely when developing MVC-based PHP web applications.

The class has three properties – name(…), action(…) and viewData(…) – and one single method – addView(…). I will try it out in a couple of projects and see if it is good enough.

Here is the code:

<?php
class Controller
{    
 /**#@+
 * @ignore
 */
 private static $_currentViewData;
 private static $_action = "";
 private static $_name = "";
 private static $_viewData = array();
 /**#@-*/

 /**
  * Get/set the name of the current action.
  *
  * @access    public
  *
  * @param    string    $value    Optional set value.
  * @return   string              The name of the current action.
  */
 public function action($value = "")
 {
    if(func_num_args() != 0)
       Controller::$_action = func_get_arg(0);
    if (Controller::$_action)
       return Controller::$_action;
    if (!array_key_exists("action", $_GET))
       return "";
    return $_GET["action"];
 }

 /**
  * Get/set the name of the controller.
  *
  * @access    public
  *
  * @param     string    $value    Optional set value.
  * @return    string              The name of the controller.
  */
 public function name($value = "")
 {
    if(func_num_args() != 0)
       Controller::$_name = func_get_arg(0);
    if (Controller::$_name)
       return Controller::$_name;
    if (!array_key_exists("controller", $_GET))
       return "";
    return $_GET["controller"];
 }

 /**
  * Get/set view data.
  *
  * @access    public
  *
  * @param        string    $key        Optional key value, otherwise current, view specific data.
  * @param        string    $value    Optional set value.
  * @return    string                     The view data value.
  */
 public function viewData($key = "", $value = "")
 {
    if (func_num_args() == 0)
       return Controller::$_currentViewData;
    if (func_num_args() == 2)
       Controller::$_viewData[$key] = $value;
    return array_key_exists($key, Controller::$_viewData)
       ? Controller::$_viewData[$key]
       : null;
 }

 /**
  * Add a view to the page.
  *
  * If ~/ is used in the view path, Wigbi will auto-parse it to the
  * application root path.
  *
  * @access    public
  * @static
  *
  * @param    string    $viewPath    The path to the view file.
  * @param    string    $viewData    Optional view data; default null.
  */
 public static function addView($viewPath, $viewData = null)
 {
    Controller::$_currentViewData = $viewData;

    $viewPath = str_replace("~/", Wigbi::serverRoot(), $viewPath);
    require $viewPath;

    Controller::$_currentViewData = null;
 }
}
?>

In the application root, I place an .htaccess file with the following content:

Options -Indexes
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-zA-Z0-9_\-]+)/?$                   controllers/$1Controller.php?controller=$1                        [NC]
RewriteRule ^([a-zA-Z0-9_\-]+)/([a-zA-Z0-9_\-]+)$   controllers/$1Controller.php?controller=$1&action=$2    [NC]
Advertisements