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
/**
* Wigbi.PHP.Controller class file.
*
* Wigbi is free software. You can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wigbi is distributed in the hope that it will be useful, but WITH
* NO WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Wigbi. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* The Wigbi.PHP.Controller class.
*
* This class represents a general controller that can be used in an
* MVC-based application. The class is static, since only one single
* controller can exist during a request.
*
* This class is really basic as of now, since it is being tried out
* in various applications.
*
* The control is automatically initialized with query variables, as
* such:
*
* <ul>
* <li>Controller::name() - by default $_GET["controller"]</li>
* <li>Controller::action() - by default $_GET["action"]</li>
* </ul>
*
* If this pattern is not used, the controller class can be manually
* initialized with the init method, which overrides the name/action
* properties.
*
* To add a view to the page, which can be done in the controller as
* well as in any view, just use the Controller::addView(...) method.
* To expose view specific data to the view, simply provide the data
* to the view with the second parameter.
*
* Use the Controller::viewData(...) property to work with view data.
* If the property is accessed without any parameters it will return
* the view specific data, if such data has been set. Otherwise, the
* property can be used as an ordinary get/set property.
*
* For the class to be available in the controller file, remember to
* include the wigbi/wigbi.php include file at the very first line.
*
*
* @author Daniel Saidi <daniel.saidi@gmail.com>
* @copyright Copyright © 2010, Daniel Saidi
* @link http://www.wigbi.com
* @package Wigbi
* @subpackage PHP
* @version 0.5
*
* static
*/
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;
}
}
?>
The Controller class will be available in Wigbi 1.0.
Oh, and 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]
Hi Daniel!
Just to let you know, when Wigbi is run on an installations of PHP in CGI mode, the following error is generated: Invalid command ‘php_flag’, perhaps misspelled or defined by a module not included in the server configuration. ‘php_flag’ is fine in PHP as a module, however.
My solution was to remove the magic_quotes_gpc line in wigbi/.htaccess, though I’m not positive this is the best approach for everyone (i.e. magic quotes are disabled by default in PHP 3.x.x, enabled in 4.x.x and deprecated in 5.3.0).
Perhaps it would be worth your time to add checks for the current PHP installation’s mode (i.e. CGI vs module) and possibly it’s version.
I don’t know how best to approach this, but hope this information is helpful.
Thanks for a great framework!