Scratching the Surface: Getting Started with PHP Fusebox - Picking Up Where We Left Off: Setting Up the Core Files (
Page 6 of 11 )
Now that we've copied the core files into the root directory, let's go ahead
and create a simple circuit app that demonstrates the basic principles behind
the PHP Fusebox architecture.
1. Open fbx_Circuits.php. You'll
see the fusedoc XML at the top, followed by the default home circuit definition:
$Fusebox["circuits"]["home"] =
"home";
This code is identifying the $Fusebox["circuits"]["home"] nested array element
as "home". The lack of slash mark(s) in the value in quotation marks
tells us this circuit is in the root directory.
When the $Fusebox["circuits"] structure is passed, fuseaction=home.main
in the URL for instance, the application will know this circuit is in the application root, and
the core "engine" file, fbx_Fusebox3.0_PHP4.1.x.php, will include the application root's fbx_Switch.php to complete the request.
Note: Although it isn't explicitly
required to create PHP Fusebox 3 web sites, you can see how all this works
by opening the "engine" file we discussed earlier, fbx_Fusebox3.0_PHP4.1.x.php.
Checking under the hood of this file is advisable. The
code is well commented with in-line comments and FuseDocs.
Ordinarily, if we were constructing a complex web application with PHP Fusebox,
we'd add additional code to define more circuits in the application root's
fbx_Circuits.php:
$Fusebox["circuits"]["sectionOne"]
= "home/sectionOne";
$Fusebox["circuits"]["sectionTwo"]
= "home/sectionTwo";
There could be as many circuits as you want. For our purposes, we'll
only be working with two circuits: the "home" and "biography" circuits.
Change the code of fbx_Circuits.php to read:
$Fusebox["circuits"]["home"] =
"home";
$Fusebox["circuits"]["biography"]
= "home/bio";
The
circuit you have just defined below the "home" circuit could be accessed through
either the URL or some kind of form action (POST or GET). For our purposes,
we'll be calling our circuits through simple text links.
2. In the root directory of your application, open fbx_Switch.php.
This is where you'll define all the fuses
in this, the home (or root) circuit. Like I said before, you'll be using
the switch / case statement in this file to include files based on the fuseaction
passed to the application.
The default code looks like this:
switch($Fusebox["fuseaction"]) {
case "main":
case "Fusebox.defaultFuseaction":
include("dsp_main.php");
break;
default:
print "I received a fuseaction called <b>'" . $Fusebox["fuseaction"]
. "'</b> that circuit <b>'" . $Fusebox["circuit"]
. "'</b> does not have a handler for.";
break;
}
The switch statement switch($Fusebox["fuseaction"]) receives
the $Fusebox["fuseaction"] variable from the URL, a form ñ- whatever you want
to use to pass it - and passes the fuseaction down through the case statements until
it finds one that matches. When a match is encountered, the matching
case statement specifies which files are included.
In this situation, if the "main" fuseaction were specified, the file dsp_main.php
would be included. If no fuseaction were passed, the same
file would be included because the default case statement calls the same fuse.