I was looking into MVC patterns but it soon became clear a full-blown OOP system with datamappers, filters etc is still a bit over my head
However, some time ago Arborint gave an example of a front controller. The way that code can be used makes it really easy. In fact, if I take this FrontController class, the only thing I would have to change ("refactor") from my procedural-coded normal pages is that instead of those pages I would have "actions".
Code: Select all
<?php
class FrontController{
var $action_dir;
var $default_action;
var $error_action;
var $action_param;
var $action = '';
function FrontController($action_dir='action', $default_action='home', $error_action='error', $action_param='action'){
$this->action_dir = $action_dir;
$this->default_action = $default_action;
$this->error_action = $error_action;
$this->action_param = $action_param;
}
function & commandFactory($action){
$obj = null;
$filename = $this->action_dir . $action . '.php';
if (file_exists($filename)) {
include($filename);
if (class_exists($action)) {
$obj =& new $action();
}
}
return $obj;
}
function execute(){
if (isset($_GET[$this->action_param])) {
$this->action = preg_replace('/[^a-zZ-Z0-9\_\-]/', '', $_GET[$this->action_param]);
} else {
$this->action = $this->default_action;
}
$obj = & $this->commandFactory($this->action);
if (! $obj) {
$obj = & $this->commandFactory($this->error_action);
}
$obj->execute();
}
}
?>Code: Select all
<?php
// configuration with basedir and database credentials
include 'config.inc.php';
include BASE_DIR . 'FrontController.php';
$fc =new FrontController('./', 'home', 'error');
$fc->execute();
?>Code: Select all
<?php
class home{
function execute(){
echo 'Home';
}
}
?>Code: Select all
<?php
class page{
function execute(){
echo 'Page';
}
}
?>I started adding other actions and came up with this:
Code: Select all
<?php
/**
* Action Contacts
* show a list of contacts
*
*/
include BASE_DIR . 'adodb/adodb.inc.php';
// this is the model class for contacts
// normally this would be a seperate file and included
class contactsmodel extends ADOConnection {
var $db;
var $result;
function contactsmodel(&$db) {
$this->db = &$db;
$this->listcontacts();
}
function listcontacts() {
$this->result = &$this->db->Execute("select * from contacts");
}
function fetch() {
return $this->result->FetchRow();
}
}
// this is the controller
class contacts {
function execute(){
$DB =& NewADOConnection('mysql');
$DB->debug = true;
$DB->Connect(dbHost, dbUser, dbPassword, dbName);
$view = &new contactsmodel($DB);
// for now just echo out the results. normally this would go to a view
while ($row = $view->fetch()) {
echo '<pre>';
print_r($row);
echo '</pre>';
}
}
}
?>Now I have a few questions.
1) Is this the right direction I have taken so far? If not, what mistakes did I make? What would be the next (small) steps?
2) I set the error reporting to strict and receive the following errors/notices
Strict Standards: Declaration of ADORecordSet_array::FetchField() should be compatible with that of ADORecordSet::FetchField() in C:\apachefriends\xampp\htdocs\project\lib\adodb\adodb.inc.php on line 3526
Strict Standards: Non-static method ADOConnection::outp() should not be called statically in C:\apachefriends\xampp\htdocs\project\lib\adodb\adodb-lib.inc.php on line 943
Has this something to do with the fact I run this code on php5?