Kaolin Fire with GUD Issues 0 through 5

kaolin fire presents :: PHP 101 :: MVC, minus the C

Breaking a php site into actions and display


Break it up!

Two things you should always do with a project so as to keep maintenance as easy as possible! First, we'll spit our file into a header and a footer; then we'll split our form into the view, control, and response.

What? You like it all in one file?

If you think about it, hello isn't a hello page until someone's entered their name, right? The first page should be more of a gateway, while the second page actually greets them. Or perhaps you could greet them in all friendliness, but you still don't know their name. So you could say "Hello, stranger." for instance. Or you could say "You are in a 10 foot by 10 foot by 10 foot room. An Orc is guarding a chest. It says it will give you all of its treasure if you tell it your name." You know, the possibilities are endless.

So then what?

First, let's split out hello into its constituent parts, because we don't want to duplicate redundant information for every page on the site. I tend to have three "global" includes in every site I do. One initializes the libraries I'm using, variables, whatever, sets up constants, blah blah blah. One prints out the "top part" of the html that is common to every page. And one prints out the "bottom part" of the html that is common to every page. That way, like the css, in order to change, say, a menu--you only have to change it in one file, and that's carried out through every file that _includes_ that file. I tend to call my files "head.php", "foot.php", and "vars.php" (short for variables). Nice and tidy in a directory listing, they're all just four character names. :)

Now you want to know what to put in each file. Start with the xml declaration and the html and all that. Go until you find the first bit of "content" that is entirely unique to the page. That is your "head.php". At the very least, it will go through to the <body> tag, but in general it should include a fair bit more. Then find the bit that stops being unique, again, towards the bottom of the file. Run with it until you get to the <html>, which is the last thing that should be sent to the client's browser. That's your "foot.php". For "vars.php", ... I don't think we need to put anything in that file, just yet. But remember to put the include before head.php. We'll be using it a lot, later.

What does your hello.php look like, now? Hopefully something like this:


Hello, .

Er, wait, who are you, again?

And here are my head and foot (note that vars is empty at the moment, but it still needs to be there for your page to work, if you're following my instructions).

head.php:

?xml version="1.0" encoding="UTF-8"?>


	Hello, split into multiple files
  


foot.php:



What else? I only have one real webpage, still.

Welcome to the world of splitting your code from your display. Let's copy the hello.php as hello2.php (I'm not feeling exceedingly inventive right now). And let's create a new file hello.do.php -- that will be our "controller" for the hello "action". This is the beginnings of an extremely light-weight model-view-controller (MVC) system. And I like light-weight. In my experience, it's very easy to pick up, and somewhat hard to screw up (though don't feel bad--everyone does, now and again). We'll worry about the "model" later, fwiw.

You should take out the bit that prints "Hello, ..." from hello.php. Put some non-dynamic text there. Just let them know that you want them to enter their name, in whatever creative or mundane manner you feel like. Just no blinking text, okay? And then take the form part out of hello2.php.

Next, update the form action to point to hello.do.php.

In hello.do.php, check to see if the variable being passed in is empty. If it is, redirect to hello.php; if not (if it's not empty, i.e. there was a name passed in), redirect to hello2.php. INCLUDE the "name" parameter to your redirect to hello2.php. Yes, this is silly, but I want you to get a solid understanding of information passing. We'll do something better in a lesson or two.

Easy peasy? Probably not. If you're not familiar with programming, or just having a bad day, you're going to make a bunch of mistakes. And hey, you don't know how to redirect, right? I'll give you a hint--use the "header" function. And, surprise of surprises, if you scroll down to the first (and only) example on that page (what page? Why, on php.net! You went there to look at the header function, didn't you?), you'll see how to do a proper redirect.

If you're not exactly sure how to pass the name parameter to hello2.php, think about what the url looked like when the form submits. That's what you want to replicate, presuming you used "get" for the form instead of "post". "post" would be hiding that information from you, essentially.

No error messages yet, but we're getting there. Redirecting is a bit of a pain, isn't it? And passing that data around everywhere. Wouldn't it be nice to hold onto the variables you've already got? On to the next lesson, then, I'll show you one answer to that that I use for errors with form validation.




I am soooo fake pre-loading this image so the navigation doesn't skip while loading the over state.  I know I could use the sliding doors technique to avoid this fate, but I am too lazy.