« 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/<span class="searchword1">clear</span>', array ('type' => 'reset', 'value'=> '<span class="searchword1">Clear</span>'));
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.
7 Comments | 
Reader Comments (7)
I’m still waiting for some interesting thoughts from your side in your next post thanks
Christian Louboutin Sandals
Christian Louboutin Sandals
[url=http://www.christianlouboutinshoesale.org/ ]Christian Louboutin Sandals[/url]
[url="http://www.christianlouboutinshoesale.org/ " title="Christian Louboutin Sandals"]Christian Louboutin Sandals[/url]
No matter what type of uggs on sale
you are looking for, there is no doubt one available for you. They have come a long way from
their days of being in World War I aircrafts. And, thanks to their manufacturer, ugg boots outlet Australia, you can
feel good while wearing these shoes while still being in style! You will simply love the
options in color, styles, and designs that you have. The whole family will be wearing ugg outlet because they just are so
darn cute and comfortable!The ugg boots
sale is one of the a lot of accepted types of cossack on the bazaar appropriate now.
Anyone who is not accustomed with them should accede bottomward one on! They are appreciably
bendable and luxurious. They action an accomplished akin of abundance and a admirable
appearance as well. They are actual balmy and cozy. Your anxiety will be in shoe heaven! Let's
yield a afterpiece attending at what is accessible in the ugg boots clearance and uggs for cheap see if we can't
acquisition something that you will enjoy!
Business owners and entrepreneurs who are determined to get the most profits realize that Chinese suppliers can give them an edge over their competitors. It is true that products are unbelievably cheap china wholesale, yet they are of good quality. The online China Wholesale Electronics business is price-driven, so you have to be able to get your merchandise as cheaply as possible. This will allow you to sell your products at a low price, yet still be able to make a good profit.
Great piece of design man !
www.superpopular.net wholesale directory can provide you with reliable china wholesale suppliers.
The reason why products from China are so cheap is because of the low labor cost there. In fact, that is why so many companies like Mattel, Nike, Apple, Adidas and many more have put up their factories there. The Chinese now have the technology and skill to manufacture different kinds of apparel, shoes, and other consumer products at very low prices. Getting your products directly from China is the surest way to get cheap goods to sell.
China is one of the most popular country to import electronics goods. China has reached his business to the top. China electronics are cheap and as well as affordable and are of good quality also.Electronics items from China such as mechanical pocket watch, Cheap Wholesale Jewelry, discount designer bags, Wholesale Electronics DropShip, wholesale mechanical watch, wholesale 925 Silver Jewelry and Hello Kitty Crystal Necklace media players are some of the hottest items and in very affordable prices. China Wholesale Electronics provide us a variety of products.It is a good site for reviewing the electronic products which are costly in our country.
<h1>discount designer bags</h1> cheap and fashion
<h1>designer inspired handbags</h1> Acclaimed
<h1>air max 2011</h1> high quality
<h1>nike shox tl3</h1> wholesale nike
<h1>women puma shoes</h1> Particular style
<h1>air max tn</h1> Different
<h1>puma shoes</h1> Very famous
<h1>puma shoes online</h1> Popular brands
<h1>women timberland boots</h1> A great feeling
<h1>wholesale gucci shoes</h1> Online Sales
Many companies have nice looking christian louboutin boots replica photos goyard luggage but when luxury watches replica rolex you omega watches replica rolex actually goyard handbags online replica swiss replica watches goyard bags price goyard pairs online rolex Fake christian goyard swiss watch brands goyard handbag replica rolex Louboutin goyards receive swiss watches replica breitling goyard shopping bag the goods,they are totally swiss watch brands replica goyard tote different than what christian louboutin daffodil was swiss army replica goyard totes bags goyard tote bag goyard goyard shopping bag bags for sale rolex represented to you on their website. christian louboutin boots thigh high Most goyard totes goyard tote bags for sale goyard bag swiss watches replica sites goyard purses swiss watch brands christian goyard bags online louboutin replica sneakers even goyard wallet don't show swiss watches replica tag heuer you the actual picture of rolex watches fake buy their replica handbags.They use authentic pictures goyard handbags which took straight from goyard tote bags original Goyard bags website of Gucci,Hermes,Louis mens watches replica rolex Vuitton,etc.At EtopMart the bag REPLICA WATCHES replica christian louboutin daffodile on the pictures is the actual knock off christian louboutin boots rolex watches fake women bag you will MEN WATCHES be receiving. We only take pictures swiss made watches from our production.We only sell and manufacture AAAAAA+ True Mirror Image Replica handbags.Our christian louboutin rolex watches fakes for sale boots knockoffs swiss watches replica rolex christian louboutin replica handbags Rolex Watches for men replica christian louboutin heels Replica christian Louboutin items are exact mirror images of the real bags.All the correct markings are at christian louboutin boots 2011 the Michael KORS Watches On Sale Rolex Watches Replicas correct places including lining, lock and keys.All watches go through quality control by our own team of experts before shipment. Not only do we check if they work right, we christian louboutin replica boots check christian louboutin boots fake christian louboutin shoes knock off christian louboutin replica pumps the straps, the screws, the case, and everything to sheer Christian Louboutin replica perfection before carefully packaging and sending them out to you. replica daffodil christian louboutin In case you do run into any problems - our watches come with a full christian louboutin shoes fake one year warranty; and if you are not happy for any reason at all with your watch - you have up to 10 days to return them.Christian Louboutin replica shoes are here to stay. Since his start in the early '90s, French designer Christian Louboutin has become Christian Louboutin replica boots synonymous with luxurious and sexy footwear. Known for their trademark glossy replica christian louboutin boots red-lacquered soles, his high-end shoes fake christian louboutin boots for women are now some of the most sought-after shoes in the world, sported by celebs like Britney Spears and Jennifer Lopez. Fortunately for the thrifty fashionista, our collection of replica shoes, inspired by the French designers Faux Chrisitan Louboutin mimic the iconic pumps and stilettos of Christian Louboutin with stunning authenticity and perfect detail. Even the most experienced connoisseur of luxury footwear would have a seriously hard time telling the replicas in our collection apart from the genuine article. Skeptical? Why not take a look for yourself. One look at this collection of fabulous Christian Louboutin replicas will make you never think the same way again about replica shoes.