580 likes | 927 Views
PHP Fusebox Tutorial By: Ben Hush. Section. Slide. Table of Contents. Section 1: What is PHP Fusebox?. 3. Section 2: Why use PHP Fusebox?. 5. Section 3: Fuses, Fuseactions and Circuits. 6. Section 4: Preset PHP Fusebox Variables. 8. Section 5: PHP Fusebox Core Files. 11.
E N D
Section Slide Table of Contents Section 1: What is PHP Fusebox? 3 Section 2: Why use PHP Fusebox? 5 Section 3: Fuses, Fuseactions and Circuits 6 Section 4: Preset PHP Fusebox Variables 8 Section 5: PHP Fusebox Core Files 11 Section 6: The Creation of a Simple App 24 Section 7: Exit Fuseactions 45 Section 8: PHP Fusebox Links 49 Section 9: Special Thanks 50 Section 10: Conclusion 51
? ? Section 1: What is PHP Fusebox? ? ? ? ? ? ? ? ?
Section 1: What is PHP Fusebox? (con’t) Fusebox is a development methodology for web applications. Fusebox was created to simplify web applications by breaking it down into numerous sections. Fusebox was named and designed after an actual Fusebox. The idea is, if one part of a Fusebox application stops working the rest should continue to work. Fusebox was originally created for Cold Fusion but was recently ported to PHP, among other languages.
Section 2: Why use PHP Fusebox? Fusebox was designed to simplify web applications in many ways. With Fusebox, complex web applications become easy to edit, understand, manage and fix. For example, let’s say that you have a business with your own web site. You are leaving the business to someone else and don’t care to explain how the website works. With Fusebox, someone with relatively no knowledge of your application can sit down and figure it out in a matter of minutes as opposed to a matter of hours or even days because everything is organized in a consistent manner that specific parts of the application can be found and edited without having to sift through the rest of the application code.
Fusebox English Section 3: Fuses, Fuseactions and Circuits Fusebox, like other languages, has it’s own terms that you must become familiar with if you want to use it. Below are a list of some of the basic Fusebox terminology that will help you to better understand what we will be talking aboutlater in this tutorial: Basic Fusebox Terminology Fuseaction Action Fuse Web page (sub) Circuit (sub) Folder
Section 3: Fuses, Fuseactions and Circuits (con’t) Like any other language, Fusebox has it’s own naming conventions. Below are a list of prefixes to add to your file names depending on what type of information they hold: • Display Fuses – Used to show Information to the user. • Select Query Fuses – Select queries. • Action Query Fuses – Insert, Update, Delete etc queries. • Location Queries – Redirect the application to a new URL • Layout Fuses – Contains layout to be used by display fuses Prefix Types of Fuses dsp qry act url lay
Section 4: Predefined PHP Fusebox Variables There are two types of preset Fusebox variables and they are both in simple array form: $Fusebox[] and $attributes[]. $Fusebox[]: This variables contain preset information and are declared in one of the fbx_ files. $attributes[]: These variables contain information that was passed using either POST or GET variables. For example if you were to echo/print $attributes[“fuseaction”] you would notice that if you also echoed $Fusebox[“circuit”] and $Fusebox[“fuseaction”] that $attributes[“fuseaction”] is a combination of the two variables (which were passed from the pervious page) and forms the “circuit.fuseaction” or “$Fusebox[“circuit”] . $Fusebox[“fuseaction”]” structure/link that was discussed earlier.
Section 4: Predefined PHP Fusebox Variables (con’t) $Fusebox["homeCircuit"]: This variable is set to the root-level circuit as defined in $Fusebox["circuits"] structure. $Fusebox["suppressErrors"]: This variable is a Boolean, which defaults to false. If true, Fusebox will attempt to give you more “smarter” that may occur from within its own code as it applies to the Framework itself. If false, you will receive the normal PHP error messages. During development you may want to turn this to true and false alternately to ensure you've got your framework set up properly. Set this to TRUE in a production environment, since at that point errors that occur will not be Fusebox framework errors but errors in your fuseactions and fuses.
Section 4: Predefined PHP Fusebox Variables (con’t) $Fusebox["circuit"]: This is the first part of the compound fuseaction that gets passed as $attributes["fuseaction"] and is evaluated in the fbx_Circuits.php file. $Fusebox["fuseaction"]: This is the second part of a compound fuseaction that gets passed as $attributes["fuseaction"]. $Fusebox["fuseaction"] is the variable expression evaluated in fbx_Switch.php. When these two variables are put into the compound variable $attributes[“fuseaction”] they form a gateway to any page in your application (circuit.fuseaction where the two variables above are represented respectively.) Note: There are more preset Fusebox variables however they are either more complicated or not necessary for basic website functions.
Section 5: PHP Fusebox Core Files The core files are the innards of a Fusebox application. Every Fusebox application needs them and without them your application will not work. Some of the core files only need to be in the root directory while others can by in more than one circuit if needed. In the next section we will look at the following files: • fbx_FuseboxXX_PHPXX.php • fbx_Circuits.php • fbx_Settings.php • fbx_Switch.php • fbx_Layouts.php Although it is not necessary to know exactly what each line of code in each filen does, it is good to have at least an idea of what they do to better understand the Fusebox methodology.
Section 5: PHP Fusebox Core Files (con’t) fbx_Circuits.php: This file is used by Fusebox to keep track of where you are in your application depending on what page is loaded. There is only one fbx_Circuits.php file in an application. Since not all the files in an application will necessarily be in the same circuit, the fbx_Circuit.php file will alias all the sub-folders for easy reference. Here is an example of an fbx_circuits.php file: $Fusebox[“circuits”][“Home”] = “your App” $Fusebox[“circuits”][“Help”] = “your App/Help” $Fusebox[“circuits”][“FAQ”] = “your App/Help/FAQ” $Fusebox[“circuits”][“Sponsors”] = “partners” As you can see, all the circuits are stored in the 2D array called $Fusebox[“circuits”][]. The names “Home”, “Help”, “FAQ” and “Sponsors” can be changed to anything you want. They are simply your name for each circuit in the array. The circuits which are listed on the right should be changed to whatever your folders are called. The name in the array does not have to be the same as the actual name of your folder. Here, I have two folders, one of which contains a sub-folder and a sub-sub-folder.
Section 5: PHP Fusebox Core Files (con’t) fbx_Settings.php: This file is quite simple and does not need to be changed much. This file is used by Fusebox to set some default values for the application. Individual circuits can contain this file only if the files in that circuit need special settings or if the values in a subcircuit need to override that of a parent circuit. Here is an example of the code you would normally change: if(!isset($attributes["fuseaction"])){ $attributes["fuseaction"] = "home.main"; } This line directs Fusebox to the default display page if a user doesn't type in a .php, .html etc file in the URL (mypage/myfolder/). Be sure to change home.main to alink suits your website. Note: home.main is in the format circuit.fuseaction so replace the two respectively.
Section 5: PHP Fusebox Core Files (con’t) fbx_Settings.php (con’t): if(!isset($GLOBALS["self"])){ $GLOBALS["self"] = "index.php"; } Changing this line makes it much easier to rename your index.php file. Since index.php is what the user will see in the URL it is not uncommon to want to change it to something more meaningful. When you change this line be sure to change your index.php file to match your new setting. $Fusebox["suppressErrors"] = false; This variable is a Boolean, which defaults to false. If true, Fusebox will attempt to give you “smarter” errors that may occur from within its own code as it applies to the Framework itself. If false, you will receive the normal PHP error messages. During the development stages you may want to turn this to alternate true and false to ensure you've got your framework set up properly but that your PHP is also . Set this to TRUE in a production environment, since at that point errors that occur will not be Fusebox framework errors but errors in your fuseactions and fuses.
Section 5: PHP Fusebox Core Files (con’t) fbx_Layouts.php: This file tells Fusebox what layout file to use for each circuit. Only one fbx_layout file is needed in a Fusebox application but if a specific circuit has a different layout it can have it’s own file. When editing the fbx_layouts.php file, use conditional logic. Here is an example of a layout file: if(isset($attributes["fuseaction"])){ $Fusebox["layoutFile"] = “myAppLayout.php”; } else { $Fusebox["layoutFile"] = “”; } $Fusebox["layoutDir"] = “” All this example is doing is checking to see if a fuseaction has been stored (meaning a page is being displayed/accessed) in the variable $attributes[“fuseaction”] then it is setting the variable which is used to display the layout ($Fusebox[“layoutFile”]) to display the proper file. If the $attributes[“fuseaction”] file is empty or a page has not been accessed, then it sets the $Fusebox[“layoutFile”] variable to nothing (so no layout file will be displayed).
Section 5: PHP Fusebox Core Files (con’t) fbx_Switch.php: This file is probably the simplest of all the core files. It tells Fusebox what pages to include based on the fuseaction by using a simple switch statement. Here is an example of an fbx_switch.php file: switch($Fusebox["fuseaction"]) { case "showIndex": include("dspIndex.php"); break; case "showNews": include("dspNews.php"); break; } This code will include one of the two dsp files depending on the fuseaction that is called for. The fuseaction is also the name of the case (my two cases are showIndex and showNews) and is stored in the variable called $Fusebox[“fuseaction”].
Section 5: PHP Fusebox Core Files (con’t) Index.php: So far we’ve only talked about index.php but not explained what it does. Index.php is the entry point for the application. All the fuseactions go through index.php. You don’t need to use the index.php file that is included but it is highly recommended. The index.php file does only one thing. It checks the version of PHP that is running on the sever and then requires in the correct fbx_FuseboxXX_PHPXX.php file included with the core files. If you know the correct version of PHP that is running on the server you can rename the correct fbx_FuseboxXX_PHPXX.php file to index.php and delete the old index.php if you want, and your application will work the same (assuming you renamed the correct file). However, keeping the index.php file provides a safety net incase your version of PHP changes. Otherwise, there is no need to change any of the code in the index.php file and you only need one copy of this file in your application.
Section 5: PHP Fusebox Core Files (con’t) fbx_FuseboxXX_PHPXX.php: This is the file that runs the show. However, we’re going to discuss the name of the file first because . This file currently exists in two forms. Those forms are: • fbx_Fusebox3.0_PHP4.1.x.php • fbx_Fusebox3.0_PHP4.0.6.php The current version of Fusebox is 3.0 so the first half of each file name says so. The second half of the file name is referring to the version of PHP that the application will be running on, either 4.0.6 or 4.1.x. Note: The beta version of Fusebox 4 for ColdFusion has recently been released, but it will probably be a while before it is ported to PHP or any other language. The important thing to understand is that this file is the engine of the application. It uses require statements to control the entire application, based on the values you set in the other files. Again, if you know what version of PHP you are using and are certain it won’t be changing anytime soon, you can delete the index.php file and rename the above file to index.php. However it is not necessary and will not change the way your website works.
Section 6: The Creation of a Simple App Fusebox was created to simplify the creation of a complex application. When we create this simple application, it may seem like it is a lot more work that simply creating a normal web page without Fusebox. If that’s how you feel, you’re probably right. However, since making a complex application would take a lot of time, I’ll simply demonstrate how to make a simple application so that you can test your newfound knowledge. The first step in building an application is to organize. This is a vital step, especially in Fusebox, because Fusebox can get confusing if you don’t write stuff down. Our first application will take a users name on a form and will display it in two different ways, one simple and one elaborate, on two different pages depending on what page the user chooses to go to. A couple good things to write down at the beginning of your application are the fuses and the fuseactions.Our application has three actions. It will take the user’s name on a form, then display the name in a simple format or an elaborate format depending on what button the user clicks.
Section 6: The Creation of a Simple App (con’t) Now we need to take those actions and abbreviate them according to our naming conventions. Since each of these actions will only require one webpage each, we will only need three fuses: • dspInputForm • dspSimpleFormat • dspElaborateFormat These fuses could have been named differently however it is always a good idea to try to make the names of the pages relative to what the pages display. And of course, since the three pages are displaying information to the user we prefix them with dsp. Now, since we have three fuses listed above, we need three fuseactions, one for each fuse. It is best to name the fuseactions similar to the relative fuses: • showInputForm • showSimpleFormat • showElaborateFormat
Section 6: The Creation of a Simple App (con’t) Now that we have our three fuses and fuseactions planned, we can now go ahead and setup the core files. First you need to put a clean copy of the core files into a directory. If you do not have a clean (unedited) copy of the core files you can download them at http://sourceforge.net/projects/php-fusebox/. It is always good to keep one copy of the core files and to simply copy and paste them into separate directories when you need to make another application. That way you don’t need to keep downloading them every time you want to make a new application. This is especially a good idea for people with dial-up internet! The first file we will want to edit is the fbx_switch file. We need to adjust the switch statement so that there is a case for each display page we have. Change the body of the fbx_Switch.php file to look like the picture on the slide after next. The changes are explained on the slide after that.
My Saved Core Files Folder As you can see I have a folder containing my core files that I can copy and past into any directory I want at any time. This way I will always have an unedited copy of the core files. This isn’t necessary but really saves a lot of time! Above: My Fusebox folder containing my tutorial, mySampleApp (the one we are about to build!) and my core files folder. Right: the inside of my core files folder.
Section 6: The Creation of a Simple App (con’t) Explaining the changes: We added the three fuseactions to the switch statement. The existing value in the first case statement, “Fusebox.defaultFuseaction”, is called when the user specifies which folder the fuseaction is in, but not which fuseaction, so if you want, you could create an error page and include it here. The default case is called if and when a user specifies a fuseaction that is not listed. (You could also include an error page here but it is not necessary. The default in this file comes with an error message which will suffice.) However, if they are just clicking links on your web page and not typing in URLs, they will hopefully never get those errors! For each fuseaction, we have used include() to include the fuses to each fuseaction. Again, don’t worry about the actual php pages. We’ll code those later. Just make sure you name them according to what you put in the fbx_switch.php file! Now we’re going to go ahead and edit the fbx_Layouts file. Try to make your layout file look like the image on the next slide. The changes are explained in detail on the slide after that.
Section 6: The Creation of a Simple App (con’t) Explaining the changes: In our application we have two possible layouts and they are based on our fuseactions. In this simple app, I don’t want any special layout on the input page but when I chose one of the different displays I want the right layout file. So in the file we just made we are checking to see what page is being displayed, then we display the correct layout file. Again, don’t worry about the actual PHP pages we’ll work on those later. Now we’re going to go ahead and edit the fbx_circuits.php file. Remember, in this file, you list all the circuits and sub-circuits involved in the application. Lucky for us, our application only has one circuit so edit your fbx_circuits.php file to look like the image on the next slide. Don’t forget to declare your folders according to what your folders are actually called. The changes are explained in detail on the slide after that.
Section 6: The Creation of a Simple App (con’t) Explaining the changes: As you can see, this file is very simple. The last part of the array, [“mySampleApp”], is the name we are going to use to call this circuit. The part at the end of the line “mySampleApp” is the location of the folder. For the root folder it really doesn’t matter what you call this. You could have called it home if that floats your boat. Now we’re going to go ahead and edit the fbx_settings.php file. In this file there isn’t a lot of changes to be done. The only line you have to edit is the following: if(!isset($attributes["fuseaction"])){ $attributes["fuseaction"] = “home.main"; } The second line that you can edit, but don’t have to, is the following: $Fusebox[“supressErrors”] = false; Try to make your file look like the image on the next slide. Note: the first line you need to edit is wrapped in the image but is actually all supposed to be the same line. The changes are explained in detail on the slide after that.
Section 6: The Creation of a Simple App (con’t) Explaining the changes: The first line sets the default page to load when you open the circuit of the application but don’t specify a specific fuse. The second line didn’t need to be edited. It simply suppresses PHP error messages for Fusebox error messages and vice versa. Now we are going to make the application-specific pages. First we’ll make dspInputForm.php. This page is a simple input form. Try to make your dspInputForm.php look like the image on the next slide but feel free to have as much fun with it as you want. As long as the base components are there, you’re set. Note: Sorry about the wrapped text. I wanted to make my HTML big enough to be readable so I had to wrap it to fit into the screen.
Section 6: The Creation of a Simple App (con’t) Now we’re going to write the dspSimpleFormat.php and dspElaborateFormat.php pages. They are quite simple so I will put the image for each on the next slide. Try to make make yours look like mine.
dspSimpleFormat.php & dspElaborateFormat.php dspSimpleFormat.php dspElaborateFormat.php
Section 6: The Creation of a Simple App (con’t) Now we need to write the layout files for the two layout pages, laySimpleFormat.php and layElaborateFormat.php. Try to make your pages look the same as my images on the next two slides.
Section 6: The Creation of a Simple App (con’t) We have edited the core files and created all the fuses necessary for our application. Now it’s time to run our program and see how well it works. The URL to run your application should be something like \\YourServerPath\mySampleApp\index.php. If you have problems and need more information to find your errors, set the $Fusebox[suppressErrors] variable to true/false in the fbx_settings file depending on what it’s already set to. Note: I had problems with the page displaying the green border that I inserted in dspElaborateFormat.php while viewing it with Netscape 4.x.x. It should work in Netscape 6 but, if you can, view this applet in Internet Explorer (or equivalent) and see if the border will display. Otherwise, change the tag to something suitable to Netscape.
Section 6: The Creation of a Simple App (con’t) Now for a little troubleshooting… If you got your application up and running you might be wondering why the first page, dspInputForm.php has the double green border even though on an earlier slide I said that I didn’t want any layout for that page. Compare our current fbx_layout.php to the one on the next slide and examine the code. As you can see, in our old file we told PHP that if the simple format page was being viewed, to display the simple format. Otherwise, regardless of what page is being viewed, display the elaborate format. There is no statement saying that we don’t want a layout for the input form. Change your fbx_layouts.php file to look like the one on the next slide, save, and parse your website.
Section 7: Exit Fuseactions So we just finished building our own simple application. To make the application understandable, only the basic Fusebox tools were used. The great thing that Fusebox allows us to do is to use a circuit in more than one application. To do this however, our application will have to be more “parameterized”. Every fuse in a Fusebox has another fuseaction that it calls. The fuse may call this new fuseaction through an <a href> URL variable or through the action parameter of a <form> tag. Each of these actions is called an Exit Fuseaction, is abbreviated as XFA and is stored in the $XFA[] array. In mySampleApp, the dspInputForm page can send you to one of two fuseactions: • mySampleApp.showSimpleFormat or • mySampleApp.showElaborateFormat Instead of referring to those specific fuseactions in our application, we will replace them with variables so that they can be changed whenever we want to change them. This will help if we want to use the same fuse in another application but want the fuse to send the user to a different fuse using a different fuseaction. We’re going to make some changes to dspInputForm.php and fbx_switch.php. The changes are in blue.
Section 7: Exit Fuseactions (con’t) Now we’re done editing our application. Boot up the old web browser and type in the same URL as before. Hopefully all goes well. If not, go over the steps again and make sure you made all the proper adjustments to your code. If you need to, change all the code back so there are no Exit Fuseactions and then try again from the beginning of Section 7.
Section 8: PHP Fusebox Links Here are most of the PHP Fusebox-related links on the web. There aren’t many but these ones are pretty helpful. Fusebox Homepage (Now supports Fusebox in PHP too!) http://www.fusebox.org PHP Fusebox Homepage, Newbie Guide to PHP Fusebox http://bombusbee.com Core Files http://sourceforge.net/projects/php-fusebox/ Core Files and Demo Apps http://php-fusebox.sourceforge.net/index.php?fuseaction=downloads.main Another Tutorial http://www.devshed.com/Server_Side/PHP/Fusebox/ My copy of mySampleApp (If you want to compare it to yours?) http://cllrnet.ca/~ben/Fusebox/mySampleApp/index.php
Section 9: Special Thanks Although I like to think that I know quite a bit about PHP Fusebox, I didn’t learn it all on my own. I’d like to give a special thanks to the following people: David Huyck – for all your time and help through the newsgroups and email. sourceforget.net – for hosting some great PHP Fusebox examples. Steve Grevemeyer – for converting Steve Nelson’s Simpsons example to PHP. Mike Britton – for his awesome tutorial that I used to learn Fusebox for PHP and also as a reference for my tutorial. Aaron Finkenzeller – my boss, for making me learn PHP Fusebox, and supporting me when I was discouraged and wanted to drop it. Lastly, Thanks for the help and support of the people on the PHP Fusebox yahoo! newsgroup and to anyone who has ever made a PHP Fusebox example or tutorial because I probably used it!