« QCon London, 2 Weeks and Counting... | Main | Lights... Camera... Action! »

March 12, 2008

Thoughts on PHP Frameworks - Part 1 - CakePHP

Over the past year I have done quite a bit of work using the .NET Framework on various projects, so when the opportunity came up to try a project in PHP I thought it would be a nice change of pace. The project involved modifying an existing open source project for internal use. The project was built on a XAMP stack using a custom built PHP framework. After a few months of craziness the project was completed but at the end of it all I was left wondering if there was a better way to complete a PHP project. The custom PHP framework was a mess. It had huge switch statements that served as controllers, a templating engine that was confusing at best and models that were built on a wonky SQL engine. With that I set off and discovered that there were various PHP frameworks available that would, in theory, be used as a base to any PHP application. I thought it would be a good idea to try out a few of these frameworks and share my impressions of them. Hopefully, if I ever have to write another PHP application again, I would at least know where to start.

CakePHP

During my research no one PHP Framework came out as the best framework. That said I found the open source CakePHP framework to be a stand-out. It includes built-in Model-View-Controller (MVC) functionality, PHP 4 and 5 support, validation and authentication among other supported features.

The Blog Tutorial

After going through the website, gathering preliminary data and completing a general overview of CakePHP (side bar - note that a few of their screencasts were not working or did not have any sound) I decided to get down to business and try actually building an application myself. To start off I planned on following the CakePHP Blog Tutorial and then modify the code to see how easy it would be to add functionality.

I already had Apache, MySQL5 and PHP5 installed & setup on my computer from previous projects so all the was required for the CakePHP installation was unzipping the code base and changing the DocumentRoot setting in Apache (in the \Apache Group\Apache2\confhttpd.conf file) to point at the Cake install. Easy enough. I was able to see the CakePHP default page at this point and the database configuration was just as straightforward. By now I realized that the default homepage was not properly styled in CSS and that I had to install the Apache mod_rewrite module. The process is outlined in the tutorial but it required a bit of tinkering on my end (I did not need to include the "AddModule" line for instance) and an Apache restart before the changes took affect.

Now to start the actual coding... I opened the install as a PHP Project in Eclipse 3.1 using the PHPeclipse plug-in. I got through the tutorial quickly and had some basic CRUD pages going.

A few things I noticed during the process:

  • The model classes are very bare. For instance there are no properties that can be accessed through the class. All calls to get properties from each model object are done through method calls that use the property name as a parameter. There is alot of CakePHP magic going on in the background to get this data from the database.
  • Like all PHP application code, typos will be a horror to track down. I mistyped some model properties on purpose and the application continued running like nothing was the matter.
  • You write code directly into the CakePHP app folder. This means that the CakePHP distributable also serves as your code base. Makes for easy deployment but maybe a problem if somewhere down the line you want to upgrade your CakePHP version.

Modifications

After finishing the tutorial I decided to try the following 3 modifications to the code; create a clear button when adding/editing a post object, create a custom validator, and implement basic User Authentication that would bring a visitor to either a logged in home or a generic home.

My first task seemed simple enough and I begun by looking at the app/views/posts/add.thtml page as an example. On line 19 you can see the PHP code that is used to create the submit button as:


<?php echo $html->submit('Save'); ?>

This calls the HtmlHelper class and uses a method to magically generate the HTML to be your submit button. I started by looking in the HtmlHelper class to see if there was an obvious method that would do the clear. Nothing stood out so I then turned to the CakePHP API Documentation. I took a look at the HtmlHelper class there also, and realized that the API Documentation was based off the code comments.
Fortunately a quick Google search turned up this. Afterwards, a quick search through the codebase revealed that the CakePHP version I had did not have the FormHelper::button() method. This resulted in my attempt to use the same code from the bug tracker page;

$html->input('User/clear', array ('type' => 'reset', 'value'=> 'Clear'));

but the resulting form button did not work. I suspected that it was the first parameter in the method call that was causing the issue. After all I did not have User class with a clear property. The generated HTML looked like this:

<input type="reset" id="UserClear" value="Clear" name="data[User][clear]"/>

So it appeared that the input tag's name was getting set to an array value that I didn't have. However I was not able to find a string ('', NULL, 'Post/title") that would make the clear button work. I'm sure that the functionality is available... but I could not spend any more time exploring the details to figure it out.

Next up was to try some Custom Validation on the Add/Edit Post page. The Blog Tutorial off the CakePHP site outlines the basics for creating Custom Validation so I followed that. On the Add and Edit html pages I added the following field:

<p>
        Test:
        <?php echo $html->input('Post/blogtester', array('size' => '40'))?>
        <?php echo $html->tagErrorMsg('Post/blogtester', 'Must start with Blog.') ?>
</p>

In the Post Class I already had the $validate array that was created when I did the tutorial, so I modified it to look like this:

var $validate = array(

    'title' => VALID_NOT_EMPTY,
    'body' => VALID_NOT_EMPTY,
    'blogtester' => '/^Blog++.+$/'

);

The validators work using Perl regular expressions. In the above code I check to make sure that the entry in the blogtester field starts with the string "Blog". I added the blogtester field to the database and magically the validation on the Add and Edit pages was working fine. On a side note the actual blogtester value was not actually written to the database at first. I couldn't figure it out but after a few attempts it magically started working! Overall it was a painless process.

Finally I wanted to try to implement some basic User Authentication on the system and maybe point the Users at different landing pages. I followed the tutorial found here that demos some of the authentication abilities of CakePHP. It should be noted that on the tutorial it specifically mentions that the code there should not be used as a basis for any type of security and that it only shows what is possible with CakePHP. Following the tutorial I created the User model, controller and login html page. I modified the AppController base class to include the checkSession() method outlined in the tutorial. I then added into the PostController the beforeFilter() method which is similar to an on_load event.
When I tried to hit the Post view afterwards the authentication code stepped in properly but was not able to redirect me to the User view. I got a "The requested address was not found on this server." error from the URL http://localhost:8080/users/login. I checked the obvious spots first. I made sure the Controller method, the view page and model all conformed to the CakePHP standards. I tried other things like restarting Apache and MySQL but again nothing. I gave up. If I'm going to write more of these articles, I need to move on.

Impressions and Conclusion

Overall I was surprised at the level of refinement and how far along CakePHP was in development. CakePHP was very easy to setup and is designed to be very easy to deploy other applications with. The use of convention over configuration in CakePHP aides in easy setup as well as providing some very nice MVC magic so that getting a base application going is very simple. Additionally I found the code documentation as well as the references at CakePHP numerous and helpful. On the downside I can see long term development on the CakePHP platform being a painful process. The combination of a convention-based Framework coupled with the a dynamic language such as PHP would be a nightmare to debug. For anyone prone to typos, like most developers, this will be a major problem.

In the end I found that CakePHP was much better than the custom made framework I had previously used, but I still prefer developing in the .NET or J2EE environments more. CakePHP is a good product and if you had to do a PHP project it would be a good framework to start off on. For now... It's on to other things. I have a few other PHP Frameworks that I will try to review in the future and if they can match my CakePHP experience I will be happy.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/t/trackback/2576390/26818334

Listed below are links to weblogs that reference Thoughts on PHP Frameworks - Part 1 - CakePHP:

Comments

Post a comment

Comments are moderated, and will not appear on this weblog until the author has approved them.

If you have a TypeKey or TypePad account, please Sign In


Privacy Policy| Sitemap| Contact Us

Copyright 2002-2007 Codesta LLC. All rights reserved.