Wednesday, January 23, 2008

I chose CakePHP

I decided that it would be a good idea to use a framework. Why? Because those who use frameworks swear by them. The idea is that using a framework cuts down on repetitive coding tasks, which crop up most frequently with database-backed web applications (read: every site worth visiting). But they are very complicated to a beginner like me.

Why did I choose CakePHP instead of Zend Framework or Symfony? Well, I installed Symfony and CakePHP, both seemed to work well enough. Users of both frameworks seem to agree that the Symfony documentation is more complete, and each eyes the other as a worthy competitor (compare this to the Ruby world, where Ruby on Rails is virtually the only choice of framework). Basically, I completed the CakePHP blog tutorial, it worked, and I was hooked. No other reason than that. So there you have it: This web application will be built using the CakePHP framework.

Once I installed it and got it running properly, I tried to go through the manual, but many problems crept up, and I found myself copying code out of the tutorial without understanding ANY of it. It reads nicely: $this->Song['id']->save();. Save this song ID, right? Well not exactly. Turns out Cake basically obliges its users to code in PHP, and to do so in completely object oriented (OO) manner. Object oriented programming is great (so I've been told), but, just like everything else in PHP, I'd never done it. So I did a few google searches, completed a few tutorials, and got a loose understanding of the syntax and purpose of OOP. That made a huge difference. For example, I learned the difference between "->" (navigating inside an object) and "=>" (navigating inside an array). But I still wasn't really grasping CakePHP and all of the "$this" commands, until I had a major breakthrough: "print_r($this)." This outputs the contents of $this on to a web page so you can see the object, and navigate through it with your eyes. So simple, so important, yet I had never done it. More later.

Thursday, January 3, 2008

Installing Apache and PHP

Before I can do anything with PHP, my computer needs to be able to recognize it, so I have to install it.

Installing PHP

The simplest way for me was downloading a Windows installer of the most recent version (5.2.5 in my case) and running the installer. You don't need to do anything fancy, just locate the installation where you would put all your regular program, such as in c:\Program Files\PHP. No problem, I thought. But if you write some PHP code, call it test.php and open it with a web browser, the browser will just show you the code, exactly as you entered it. That's because PHP is a server-side scripting language, so there must be a server to recognize it and parse it. Enter the "A" part of the LAMP stack: The Apache web server.

Installing Apache

To get Apache, download the windows installer, run the installer, and install it wherever you like. The tough part is configuring it, which is done through the httpd.txt file, which in my installation is located at C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.txt. When you open this file you are confronted with a mess of hash marks (#) and scary-looking commands. The hash marks are actually there to cancel out the commands that they precede, which means that in order to enable a command, just delete its hash mark. Anyway, here's the important stuff:
  • ServerRoot "C:/Program Files/Apache Software Foundation/Apache2.2/" - this has to point to the directory where you installed Apache. If you used the windows installer it should be properly filled out.
  • ServerName localhost:80 - this what you put if you're using your own computer as a server, which you are if you're installing it on your own machine. Open a web browser and type in "localhost," and you're accessing your own Apache server, in essence serving a web page to yourself. "80" refers to the port, which is probably set by default if you used the windows installer.
  • DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs" - this points to the folder that "localhost" becomes. Anything in the folder is being hosted by Apache. So if you put a file called "index.html" in the "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/" folder, then you can open it by pointing a browser to "http://localhost/index.html." Neat.
These settings are probably enabled by default (as well as a slew of others), if you used the windows installer like I did. Test the installation by opening a browser and typing in "localhost." If it says "It Works!" then the server is working. Remember that "localhost" just points to the server, which opens C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\index.html. So you can edit index.html to make it say anything you want it to, or even replace it with another file called index.html. "localhost" will point to it every time.

If add a bit of PHP code to index.html, the server won't recognize it unless you have installed and configured PHP. If you open the file via "localhost" you won't see any difference. The file will look the same as if you had opened it from the httpd file folder, i.e. not running on a server.

Back to PHP

So now the PHP installation comes in. If you run the windows installer and the program executes, it added a bit of code the very bottom of the config.txt file (C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.txt).
PHPIniDir "C:/Program Files/PHP/"
LoadModule php5_module "C:/Program Files/PHP/php5apache2_2.dll"
These two lines tell Apache where PHP installed its .ini file, and it told Apache to load another module (the php5_module) and where to find that module (in the PHP installation directory). The php.ini file is just like Apache's config.txt file - it's a list of commands, most of which are blocked by comment characters (semi-colons instead of hashes). If you used the windows installer, these commands should be set correctly. Test the installation by adding some php code to the index.html file, as above. If the browser render the php code correctly (i.e. without the php tags) then PHP is installed and working.