Page 1 of 1

Pattern to apply to this -- Command?

Posted: Wed Aug 09, 2006 1:52 am
by Luke
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

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());
}
?>
Index.php

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();
?>

Posted: Wed Aug 09, 2006 6:05 am
by Ollie Saunders
You should probably put related pages in a class. So you could have a class called Access with login and logout methods. You can then use protected methods to do anything you find you do regularly like this:

Code: Select all

$request = $Registry->get('Request');
$Session = $Registry->get('Session');
$Filter = $Registry->get('Filter');

$Template->assign('sub_title', 'Log in');

Posted: Wed Aug 09, 2006 2:30 pm
by Luke
8O Bleh~!

Here is a quick list of things I need to accomplish:

Check whether user is logged in
Log user into system (if necessary)
Locate the model(s) we're working with (if any).
Send model to the view (set all template variables).
Locate & load templates that are necessary for this action.

I was thinking of implementing the command pattern and doing like that...

So... set up a class that accepts "command" classes (children classes of an abstract "command" class) and executes them (possibly based on the results of the last command)

Advice?

Posted: Wed Aug 09, 2006 3:09 pm
by Nathaniel
So... set up a class that accepts "command" classes (children classes of an abstract "command" class) and executes them (possibly based on the results of the last command)
You want the front controller to take care of instantiating the Command, probably with an abstract factory. Your front controller could possibly take care of:

Check whether user is logged in
Log user into system (if necessary)

- Nathaniel

Posted: Wed Aug 09, 2006 4:32 pm
by Luke
I posted my front controller in code critique... please refer to that post... viewtopic.php?t=53335&highlight=