Pattern to apply to this -- Command?
Posted: Wed Aug 09, 2006 1:52 am
I am trying to think of what pattern I could apply to make this more efficient and scaleable
The following is one of my action pages which is controlled by a front controller (which I think is wrongly implemented). Suggestions on making this whole thing better would be appreciated:
I read about the command patter which seems to fit the front controller, but I'm not sure.
login.inc.php
Index.php
The following is one of my action pages which is controlled by a front controller (which I think is wrongly implemented). Suggestions on making this whole thing better would be appreciated:
I read about the command patter which seems to fit the front controller, but I'm not sure.
login.inc.php
Code: Select all
<?php
$request = $Registry->get('Request');
$Session = $Registry->get('Session');
$Filter = $Registry->get('Filter');
$Template->assign('sub_title', 'Log in');
$Template->assign('login_logout_link', '');
if($user_id = $Session->get('user_id')){
$Template->assign('login_logout_link', '<a href="' . APP_DIR . 'logout">Log out</a>');
$Template->assign('form', "You're already logged in.");
}
else{
$Form = new HTML_QuickForm('login');
$Form->addElement(new HTML_QuickForm_text('username', 'Username: '));
$Form->addRule('username', 'Please enter your username', 'required');
$Form->addElement(new HTML_QuickForm_password('password', 'Password: '));
$Form->addRule('password', 'Please enter your password', 'required');
$Form->addElement(new HTML_QuickForm_submit('Login', 'Login'));
if($Form->validate()){
$Registry->register('AuthorizorUsernamePassword', null, $Registry);
$authorizor = $Registry->get('AuthorizorUsernamePassword');
$User = new User();
if($found = $User->load("username=" . $db->qstr($Form->exportValue('username')))){
$authorizor->setMatch($User);
$credentials = $Registry->get('CredentialUsernamePassword');
$credentials->setUsername($Form->exportValue('username'));
$credentials->setPassword($Form->exportValue('password'));
$Registry->register('Authenticate', null, $authorizor);
$authenticate = $Registry->get('Authenticate');
$user_id = $authenticate->isAuthorized($credentials);
$Session->set('user_id', $user_id);
}
if(isset($user_id) && $user_id){
$Response->setRedirect('home');
}
$Template->assign('message', '<p class="error">Your login was incorrect.</p>');
}
$Template->assign('form', $Form->toHTML());
}
?>Code: Select all
<?php
require("config" . DIRECTORY_SEPARATOR . "config.inc.php");
require('loader.inc.php');
require_once CLASS_PATH . 'adodb/adodb.inc.php';
require_once CLASS_PATH . 'adodb/adodb-active-record.inc.php';
$db = NewADOConnection(DSN);
ADOdb_Active_Record::SetDatabaseAdapter($db);
$db->debug = (DEBUG);
$Registry->addRegistered($db, 'MySQL');
$Template->assign('page_title', 'Page Title Goes Here');
$Template->assign('login_logout_link', '<a href="' . APP_DIR . 'login">Log in</a>');
$content = "";
require($Front->getActionPage());
$Template->assign('content', $content);
$page = 'default';
$Response->addContent($Template->fetch('elements/header.tpl'));
$Response->addContent($Template->fetch('views/' . $action . '/' . $page . '.tpl'));
$Response->addContent($Template->fetch('elements/footer.tpl'));
if(DEBUG){
ob_start();
echo "<pre>";
print_r($Registry);
if (isset($_SESSION)) print_r($_SESSION);
echo $db->errorMsg();
echo "</pre>";
$Response->addContent(ob_get_clean());
}
$Response->out();
?>