Showing posts with label Quentin Zervaas. Show all posts
Showing posts with label Quentin Zervaas. Show all posts

Monday, August 24, 2009

The Lost Weekend: IIS 7, PHP, and Zend

My plan for this weekend was to implement a new version of one of my web sites. I wanted to move from an old complicated build process to one based on using the Zend conventional modular layout. I estimated I could complete the task over the weekend. I was wrong.

Microsoft made a significant change by allowing IIS to run on the home versions of Vista. I could install IIS 7 on my new Vista 64 bit PC. I thought it would be a fun learning experience to use IIS 7 as a test bed while I implemented the changes. I was wrong.

Microsoft packaged up PHP and created an install using their new Web Platform Installer. This sounded like a quick way to get PHP up and running. If you want to get PHP up and running quickly on IIS 7, the Web Platform Installer will do that. However, if you want to use PEAR, the Web Platform Installer is a dead end. I have a problem with PEAR. I was unable to find any instructions on how to install PEAR manually. Everywhere I looked the documentation always said to run the "go-pear" script or batch file. Microsoft decided not to include that little file in their installation of PHP via the Web Platform Installer. When you install PHP via the Web Platform Installer on Vista 64 PHP is installed in the "\Program Files (x86)\php" directory. The installer then sets the include path to ".;c:\php5\pear" even though there is no php5 directory created. If you want PEAR and PHP on IIS, you want to avoid using the Web Platform Installer. Instead, use the "installer version" found at "windows.php.net". When you install, make sure you change the installation directory to "\php5". I also recommend you install PEAR, which is an option with the "installer version". I thought the Web Platform Installer quickly set up PHP and PEAR. I was wrong.

When you install IIS 7, it does not include a URL Rewrite Module. Only two things were quick and easy this weekend. The first was installing the IIS URL Rewrite Module and importing the rewrite rules from Apache. The second was downloading and installing the Zend Framework. The Zend Framework is a .Zip file you download, and decompress to anywhere you choose.

I used the Zend Framework before. I never started a project using MVC from scratch. I wanted to use the conventional modular layout to support three levels. There were tutorials that showed how to set this up. It looked very simple to me. I was wrong.

In previous entries, I mentioned "Practical Web 2.0 Applications with PHP" by Quentin Zervaas. Apress published the book in December of 2008. It is already out of date. About the time the book was published version 1.8 of the framework was released which deprecated the use of Zend_Loader's autoloader. I thought I could use the tutorial in the book by Zervaas to build a site from scratch. I was wrong.

I set up my site with a single controller. I kept getting the error "Invalid controller specified". I spent hours checking my directory structure. I googled the error message. Most web sites suggested that there was a problem with the file or directory casing. I am using IIS 7 on Vista. Files and directory casing should not be an issue. I thought I had some sort of typo in some file. I was wrong.

Finally, after much pain and cursing I found the problem. The problem is auto-rendering. The Zend framework automatically goes hunting for the template with auto-rendering enabled. Most of the sample code has a hidden assumption that auto-rendering is off. This is no longer true with the default configuration. I was getting the error "Invalid controller specified". The problem was not with the controller but with the view. Here is a situation where the MVC separation in Zend is poorly constructed. Zend generates an inappropriate error message.

Overall, I lost a weekend getting IIS 7, PHP, and Zend working. I did not finish redesigning my website. I still have a lot of work ahead. I was not expecting a major learning experience this weekend. I was wrong.

Sunday, May 24, 2009

Problems with Practical Web 2.0 Applications with PHP

I wanted to use the code from Quentin Zervaas's book for a website. There is a lot of good material in the book. Unfortunately, the development environment used by Quentin Zervaas must be very different from my development environment.

I could not get the code to work for days. I kept fumbling around and fumbling around trying to figure out what was wrong. The first problem was the index page code was crashing and leaving no log. It took me a while, but the problem was with the EmailLogger code bye Zervaas.

I am running on a Fedora 10 Linux development server. This is a minimally configured virtual machine running on my under powered VM server. This is not a production server. The first problem comes from the minimal configuration. Zervaas wrote the following code in the file "index.php":

$writer = new EmailLogger($_SERVER['SERVER_ADMIN']);

The code is simple enough it passes the "SERVER_ADMIN" name to the EmailLogger function. On a minimally configured system the value of "SERVER_ADMIN" is "root@localhost".

The EmailLogger function calls Zend_Validate_EmailAddress to make sure the email address is valid. The code is shown below:

$validator = new Zend_Validate_EmailAddress();

The default constructor for the Zend_Validate_EmailAddress only allows DNS hostnames. This means that "root@localhost" is not a valid email address using the default parameters when creating Zend_Validate_EmailAddress. This took a long time to figure out.

The solution is to tell the constructor to allow both DNS and local names. The code to do that is below:

$validator = new Zend_Validate_EmailAddress(Zend_Validate_Hostname::ALLOW_DNS | Zend_Validate_Hostname::ALLOW_LOCAL);

The support site for "Practical Web 2.0 Applications with PHP" does not document the problem with the email validation. The support site did document the next problem I ran into. This was extra slashes at the beginning of each URL. Instead of the URL being "/index" it was "//index". I tracked down the problem quickly. But every solution that made sense to me failed. The support site has two lists of errata for the book. One list is the old and unmaintained list. The other list is the new and maintained list. The new list does not contain the errata listed on the old list.

A more subtle problem is the wording of the errata. The old and unmaintained list of errata states, "When using Zend Framework 1.0.3 URLs will be generated beginning with 2 slashes." What the errata should have said is "When using Zend Framework 1.0.3 or newer URLs will be generated beginning with 2 slashes." I installed the Zend Framework 1.7.8.

I now have a problem with Captcha and image generation. This has been yet another learning experience.