I believe in having controllers that are straightforward and relatively simple. Decoding the logic of your controller should be an oxymoron. There’s very few times that a controller needs to have very sophisticated logic.

In the example below I’m going to show how based on user inputs the system will decide what steps need to be taken by the user and how it all works very cleanly within the command structure. This is a rather simple example but hopefully you can see how it could be modified for far more complex situations. I’m also keeping the forms and structures overly-simple for demonstration purposes. I’m also assuming that I’m using a pretty simple object.

You will see in this and other examples that I often mix my views and controllers in one file. This is not an issue so long as you’re using the command structure so it’s obvious which bits and pieces are views and which are controllers. When you loose that distinction is when you really get in trouble with messy code.

<?php

include 'header.php';

$command = $_REQUEST['command'];
if($command=='') {
	$command = 'pre-eval';
}

$myObj = new my_object($_REQUEST['id_my_object']);

// This is a very basic controller that figures out where to start (or end)
if($command=='pre-eval') {
	if($myObj->getFeeling()!='' && $myObj->getProgramming()!='') {
		header("Location: ".$_SERVER['PHP_SELF']."?command=thank&id_my_object=".$myObj->id);
		exit;
	} else {
		header("Location: ".$_SERVER['PHP_SELF']."?command=step1&id_my_object=".$myObj->id);
		exit;
	}
}
		
// This is essentially a "view" that presents a form
if($command=='step1') {
	?>
		<form action="<?=$_SERVER['PHP_SELF']?>" method='post'>
			<input type='hidden' name='command' value='step1_save'>
			<input type='hidden' name='id_my_object' value="<?=$myObj->id?>">
			How are you feeling today?
			<select name='feeling'>
				<option value="">Please Select</option>
				<option value="Great">Great</option>
				<option value="OK">OK</option>
				<option value="Awful">Awful</option>
				<option value="Unsure">I dunno</option>
			</select>
			<br>
			<label>
				<input type='checkbox' name='doStep2' value='true'> I want to answer more questions
			</label>
			<br>
			<input type='submit' value='Continue'>
		</form>
	<?
}


if($command=='step1_save') {
	$myObj->setFeeling($_REQUEST['feeling']);
	$myObj->Save();
	if($_REQUEST['doStep2']=='true') {
		header("Location: ".$_SERVER['PHP_SELF']."?command=step2&id_my_object=".$myObj->id);
		exit;
	} else {
		header("Location: ".$_SERVER['PHP_SELF']."?command=thank&id_my_object=".$myObj->id);
		exit;
	}
}

if($command=='step2') {
	?>
		<form action="<?=$_SERVER['PHP_SELF']?>" method='post'>
			<input type='hidden' name='command' value='step2_save'>
			<input type='hidden' name='id_my_object' value="<?=$myObj->id?>">
			Do you like programming?
			<select name='like_programming'>
				<option value="">Please Select</option>
				<option value="Yes">Yes</option>
				<option value="No">No</option>
				<option value="Unsure">I dunno</option>
			</select>
			<br>
			<input type='submit' value='Continue'>
		</form>
	<?
}

if($command=='step2_save') {
	$myObj->setProgramming($_REQUEST['like_programming']);
	$myObj->Save();
	header("Location: ".$_SERVER['PHP_SELF']."?command=thank&id_my_object=".$myObj->id);
	exit;
}

if($command=='thank') {
	?>
		Thank you for completing the questions.  You said:<br>
		Feeling: <?=$myObj->getFeeling()?>
		<? if($myObj->getProgramming()!='') {
			<br>
			Programming: <?=$myObj->getProgramming()?>
		}
		
	<?
}

?>

August 12th, 2013

Posted In: Controllers

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: