Thursday
Jul312008

« Thoughts on PHP Frameworks - Part 2 - Zend Framework »

Over the course of my time here at Codesta I have been exposed to various platforms and technologies. On many occasions I have had to work on a PHP application that already had its own custom framework in place. One project in particular scarred me horribly; the Model-View-Controller (MVC) logic was muddled together and a pain to work with. Overall the framework the application was built on was a total mess.

This article is part two in my quest to see if there are any good PHP Frameworks out there that can compete with established frameworks like J2EE and .Net. In the first part (found here) I reviewed the CakePHP framework. Now I will be documenting my experience in getting a simple Zend application running.

Zend

Zend is one of the oldest Frameworks available for PHP; it is a part of the Zend Platform by Zend Technologies which specializes in PHP development. I attempted to focus in on the Zend Framework itself but at the same time I downloaded and tried out the Zend Studio. It should be noted that a few of their products (like the Zend Studio) need to be purchased after the trial period is up.

Install and Setup

The Zend Studio installation is straightforward and you can start working with it right away. The Zend Framework itself is a library of PHP classes that you include in any PHP project to use. Zend Studio has the ability to create a project that has the proper directory stucture and the Zend Framework code setup for you already.

First Application

With everything installed and ready to go I was set to try building my first application. As with my last blog article I decided the best way to test out the framework would be to follow a tutorial and then see how easily it would be to modify the resulting application.

I downloaded version 1.5.1 of the Zend Framework and quickly realized how new the Framework was. It was very difficult to find any tutorials that were valid for the version that I had. It seems that the Zend Framework had only reached version 1 very recently and there wasn't much community documentation available yet. There were lots of resources and tutorials on Zend in general; just not the version I had.

After praying to the Google overlords for a bit I came across a good webcast here. The tutorial also outlined how to install and run the Zend Framework without any other Zend tools as well.

Some highlights from the webcast were:

  • PHP exceptions!
  • The Zend Controller and View magic worked as expected out of the box
  • Demo on how to send custom data to the Views through the Controllers
  • Demo of the Zend Reference Guide (found here)

Mod Rewrite Problems

Although it seemed as though everything on Zend was working fine on my machine it wasn't the case. Most of my work to this point had involved working with changes to the Index View and Controller. When I started adding in other Controllers, I realized I could not get through to them via the URL like I should be able too. I tracked the problem down to a conflict between rewrite rules in my Apache httpd.conf and the .htaccess rewrite rules that come with Zend.

It turns out that a number of people have run into similar conflicts with the rewrite rules; a Google Search reveals how other people have tried to handle the problem.  As an interesting side note, at Codesta we have run into identical problems with rewrite rule conflicts when using Drupal. If you are using Zend and a have complex Apache configuration, expect to have to do some work to get Zend to work properly.

Here is how I tried to work out the problem.

The format for a typical URL in Zend is [base_url]/Controller/View/. To get to the index page you should be able to use [base_url]/ and that should take you to the IndexController with the Index View.

Up to this point I had been using http://localhost:8080/ptp/index.php to test out my changes, but when I tried something like http://localhost:8080/ptp/ I got Apache permission errors. I had setup my own rewrite rules in Apache to point to the Zend Project like this:

RewriteCond %{REQUEST_FILENAME}   ^/ptp/(.*)
RewriteRule ^/ptp/(.*)   "C:/Zend/workspaces/DefaultWorkspace/Zend_Test/html/$1" [L]

Which would forward any requests to my Zend install.

The problem was that the Zend framework has its own .htaccess file to provide setup information to Apache. The rewrite rule I had wrote broke how index.php redirects the browser to the proper Controller and View in Zend.

If you had to integrate a Zend Project as a component to a larger application that relied on Apache you could try this rewrite rule in the http.conf file:

RewriteCond %{REQUEST_FILENAME}   ^/ptp/(.*)
RewriteRule ^/ptp/(.*)   "C:/Zend/workspaces/DefaultWorkspace/Zend_Test/html/index.php" [L]

However when I attempted this configuration change I got the following exception thrown by Zend:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (ptp)' 
in C:\Zend\workspaces\DefaultWorkspace\Zend_Test\library\Zend\Controller\Dispatcher\Standard.php:249 
Stack trace: 
#0 C:\Zend\workspaces\DefaultWorkspace\Zend_Test\library\Zend\Controller\Front.php(914): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) 
#1 C:\Zend\workspaces\DefaultWorkspace\Zend_Test\html\index.php(58): Zend_Controller_Front->dispatch() 
#2 {main} thrown in C:\Zend\workspaces\DefaultWorkspace\Zend_Test\library\Zend\Controller\Dispatcher\Standard.php on line 249

The problem appears to be that the Zend project is parsing "ptp" as a controller. I stopped my attempts at ramming the Zend project into my Apache configuration and used a fresh configuration file for Zend testing. If you do this, just make sure the DocumentRoot value to point to your project.

If you want to try to solve the rewrite problem, I would look to see if there was a base URL setting anywhere you could set in Zend so that it knows where to look for the Controllers and Views in the URL. The Zend exceptions were very helpful in narrowing down the problems in this case!

Creating CRUD Pages

After finishing up the simple application I noticed that there was no mention of how to do anything on the Model Side of the Zend Framework. Considering that the Model component is a vital part of any application I thought it would be important to research.

After a bit of digging around I realized that it gets much worse. At first I was unable to find any tutorials or guidelines on setting up Models in the version of Zend that I was using. I was able to find a few tutorials for older versions of Zend; but not the exact version I was working with. I ended up thrashing around trying to figure out the differences between versions but I had a lot of trouble just getting the bootstrapping and conventions right.

Finally I stumbled onto http://www.akrabat.com and found tons of resources on Zend. Most importantly there was a tutorial (found here) that outlined an application that would store a CD collection using Zend. I ran into some configuration problems with PDO_MYSQL. The tutorial specifies PDO_MYSQL as the database adapter in the config.ini file. It took a little bit of work before I realized that PDO_MYSQL was not a Zend component but rather an extension of PHP to use MySQL. I found out how to install the extension here and I was back to work.

The rest of the tutorial was straightforward and worked for me on the first try.

Modifications

While following the tutorial, I setup the Album Controller on its own. Rather than having it as the default Index Controller I created a new controller that I tested by using the url: http://localhost:8080/html/album/.

By setting up the Album as its own controller, I had to modify all redirects in the Album Controller to point to the proper location.

For example whenever the tutorial used:

$this->_redirect('/'); 

I had to insert

$this->_redirect('/album/'); 

Then all index actions pointed back to the Album Controller rather than the Index Controller. I wanted to do more modifications to my application as well as try out the authentication tutorial at akrabat.com but time constraints forced me to abandon those tasks.

Zend Studio

At first I used the trial version of Zend Studio to see what it was like. More specifically I wanted to see how the debugger worked. As I mentioned earlier it is possible with Zend Studio to create a Zend Project with the framework and directory structure pre-defined for me. So I started writing my test application with Zend Studio.

However, after a bit of work I started going back to using Eclipse 3.1 on my Zend Application. I was never able to get away from the fact that Zend Studio is based on Eclipse and that I could be just as productive using Eclipse. Due to other projects, my trial license of Zend Studio expired before I really got into my Zend Application and I was never able to try out the debugger.

Overall, I felt that what I could do with Zend Studio, I could also do with Eclipse. Although the Zend debugger is built into Zend Studio; there are other PHP debuggers that work with Eclipse like PHP DBG.

Conclusions

In comparison with CakePHP, the Zend Framework is more mature and complete. There is more of a community and the framework is more complete. I felt that I was hampered a bit because I was using a newer version of Zend and did not have access to more tutorials and guides online. Also, I was not convinced that the Zend tools are worth the investment. Like CakePHP I was impressed at the level of work that had been invested into Zend and it is a viable, quick method to get a PHP application started.

Reader Comments (6)

During the festivals and special occasions, many stores offer discounts. So, you may get the designer coach purse of your choice at a reduced rate. So, don’t forget to step into the ugg bailey button tripletnearby stores. You may find something great at a nominal price.Every one has a favorite color. If you can find out a designer ugg boots clearance of the color you like, it will be great. Different colors also carry distinctive meanings. It will help you to ugg outlet store create positive energies around you.

Ethnic designs are becoming popular day by day. It may be out of the reach of your knowledge to specify the denotation of these arts. But it is widely accepted as a status symbol. So, don’t hesitate to possess one.Your interest should also play a major role while selecting the designer coach purses. Select as per your interest. For example,coach purse outlet if you have keen interest on nature, you can buy a piece that ugg boots sale resembles essence of nature.It should also match your dresses. Pay equal attention to this aspect also. You can easily find it out by having a look on both the dresses and the wallet.

Your designer coach purses outlet should match your personality. Please, carefully choose a ugg boots outle. You can also consult your friends or the storekeeper to find out the best one for you. If you keep in mind the following tips while buying a designer cheap coach purses, you will definitely get the value for your mind. You will also have the ugg classic tall sale mental satisfaction of possessing something great. You like to put on a coach outlet store online. Your shoes are also carefully chosen from the attractive range of a renowned brand. ugg classic tall are also widely accepted as an insignia of status. After all, you will pay from your ugg bailey button triplet sale for all of your other needs.

November 26, 2011 | Unregistered Commenterugg boots sale

Christmas is coming, combine with great happiness and long-awaited reunion…This is a festival for all of us to enjoy our life, to appreciate this year’s harvest and growth, to share all the happiness and bitters with our family, our friends. Let`s write that letter we thought of writing "one of these days", today. In order to share this happiest time in a year, our website takes the role as Santa to send you the most sincere blessing and the most meaningful gift…Come to dvdbestonline to collect what you want, and we’ll send them to your hands with totally free shipping.
Big Love on dvd

November 30, 2011 | Unregistered CommenterBig Love on dvd

Your website have very interesting article. I got knowledge from here. Besides that, your blog is so popular among the searchers from search engines. It means yours website is very goodAlarm Monitoring Houston [url=http://www.smithmonitoring.com/houston-security/]Alarm Monitoring Houston[/url]

December 5, 2011 | Unregistered CommenterAlarm Monitoring Houston

globalization and the internet now make it easier to find a legitimate supplier from china wholesale. For example, you can use superpopular wholesale directory list to find suppliers of the products you want to sell. You can rely on the suppliers found on Superpopular directory because they have been verified to be reputable. It is also best if you contact the supplier by phone and talk to a representative especially at the start to establish a good relationship with the supplier.
You must decide if you want to work directly with a China wholesale supplier or with a dropshipper. If you order from a wholesale supplier you will be importing products so there are necessary documents to accomplish and customs duties to be paid. There will also be a minimum order required. The advantage here is that you will be getting the products at amazingly low prices, so you will actually be able to get higher profits. Just make sure all importation documents and duties are in order.
Now a days most of the retailers of UK imports their wholesale products from China like wholesale electronics batteries which lowers their cost and increases profit from 25 to 30 % as compared to buying from in-house manufacturing companies. The other products of China like Wholesale Jewelry, wholesale watches,mechanical pocket watch,wholesale Hello Kitty Watches,wholesale hello kitty jewelry,China Wholesale Bags,Electronic Watch,Wholesale Electronics DropShip, cloths or shoes normally confer profit from 10 to 15 %. The benefits to manufacture your products in China are also massive. You get much more cheap manpower which is very dedicated to their work. Moreover the prices of the properties of cheap china wholesale and the machinery are also very low so you can easily get enough space for your setup. These factors allow you to produce quality products in very low price, so you can make high profits.
Sometimes a dropshipper can be a better option, because you will not have to deal with a lot of different wholesale suppliers. There is no minimum order required. Although the price will be higher, you will not have to deal with packaging and shipping, and you can cater to customers all over the world. Again, you can find reliable dropshippers on a wholesale directory list like Superpopular.

December 11, 2011 | Unregistered CommenterWholesale Suppliers China

At the same time, the investigation<h1>Coach Outlet</h1> that led the United States to <h1>Coach Outlet</h1>the bank, the Lebanese Canadian<h1>Chanel Handbags</h1> Bank, provides new insights <h1>Coach Outlet</h1>into the murky sources of<h1>Coach Outlet</h1> Hezbollah’s money. While law enforcement <h1>Chanel Bags</h1>agencies around the world <h1>Coach Factory Outlet</h1>have long believed that <h1>Coach Outlet Online</h1>Hezbollah is a passive beneficiary <h1>Coach Outlet</h1>of contributions from loyalists <h1>Coach Outlet</h1>abroad involved in drug trafficking <h1>Coach Outlet Online</h1>and a grab bag of other<h1>Louis Vuitton Bags</h1> criminal enterprises, intelligence <h1>Coach Factory Outlet</h1>from several countries points <h1>Louis Vuitton Bags</h1>to the direct involvement of <h1>Coach Factory Store</h1>high-level Hezbollah officials <h1>Coach Outlet</h1>in the South American cocaine<h1>Coach Factory Outlet</h1> trade.One agent involved in the<h1>Coach Outlet Store</h1> investigation compared Hezbollah <h1>Coach Factory Outlet</h1>to the Mafia, saying, “They <h1>Coach Outlet</h1>operate like the Gambinos on <h1>Chanel Bags</h1>steroids.”On Tuesday, federal <h1>Chanel Bags</h1>prosecutors in Virginia announced <h1>Louis Vuitton</h1>the indictment of the man <h1>Coach Factory Store</h1>at the center of the Lebanese<h1>Chanel Handbags</h1> Canadian Bank case, charging <h1>Coach Outlet</h1>that he had trafficked drugs <h1>Chanel Bags</h1>and laundered money not only <h1>Louis Vuitton</h1>for Colombian cartels, but also for<h1>Coach Factory Online</h1> the murderous Mexican gang <h1>Coach Outlet</h1>Los Zetas.The revelations about <h1>Chanel Bags</h1>Hezbollah and the Lebanese Canadian<h1>Coach Outlet</h1> Bank reflect the changing <h1>Louis Vuitton Bags</h1>political and military dynamics<h1>Coach Factory Outlet</h1> of Lebanon and the Middle East. <h1>Chanel Bags</h1>American intelligence analysts believe<h1>Coach Factory</h1> that for years Hezbollah

December 14, 2011 | Unregistered CommenterCoach Factory Outlet

<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

December 19, 2011 | Unregistered Commenterfds

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>