Pattern to apply to this -- Command?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Pattern to apply to this -- Command?

Post 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();
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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');
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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?
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I posted my front controller in code critique... please refer to that post... viewtopic.php?t=53335&highlight=
Post Reply