From Zero to Zend Framework Project in 10 Minutes

I first started working with the Zend Framework in July of 2007. The framework has come a long way since then, and one of my favorite new components is Zend Tool. Until Zend Tool came on the scene, my least favorite part of any Zend Framework project was getting the project up and running, from zero to “Hello World,” if you will. I always left out some important piece of configuration, screwed up the directory structure, or made some other equally foolish, simple mistake that kept me chasing my tail until I finally got everything set up just right. No longer! Zend Tool does all of that work for me, allowing me to get to work on the meat of my project right away.

Of course, you can’t use Zend Tool until you’ve got the framework installed. Once the framework is installed, there are very specific steps required to fire up Zend Tool. Once Zend Tool builds the project for you, you’re going to need a virtual host set up so that you can actually run the thing. No problem, right?  Well, maybe not.

It can be really challenging for the new guy to get from zero to a Zend Framework project without wanting to chunk his computer out the window (headdesk anyone?). I know it was that way for me. In the interest of saving his sanity and helping the new gal get going, I’ve tried to put all the necessary steps together in one place.

Here’s what we’re going to do:

  • Install the Zend Framework
  • Get Zend Tool up and running
  • Create a new Zend Framework project
  • Get an virtual host up and running
  • Celebrate victory!

Disclaimer

While I’m going try to stay true to my “Zero to Zend Framework Project” thesis, I am making a few assumptions. This tutorial is written for Ubuntu, PHP 5, and Apache. While I’d love to be able to address Windows, Mac, and other *nix distros, I can only share what I know.  I’m also assuming PHP 5 (PHP 5.2.4 at minimum) and Apache are both installed and functioning correctly, and Apache’s mod_rewrite module is up and running. If that’s not the case, please refer to the resources section at the end of this post to find instructions on getting everything ready to go.

Installing the Zend Framework

Installing the Zend Framework is as easy as downloading a .zip or .tar.gz file (your choice) and extracting the contents onto your machine.

  • Create a directory named phplib in your home folder.
  • Head over to Zend Framework: Downloads and download Zend Framework <version_num> Full.
  • Double click on the downloaded file, choose “Extract,” navigate to the phplib directory, and click “Extract.”

As of this writing, the most recent version of the Zend Framework is 1.10.3, so the full path to my Zend Framework installation would be

/home/jkendall/phplib/ZendFramework-1.10.3

IMPORTANT: Make sure that you downloaded Zend Framework <version_num> Full.  That will be important later.

TIP: As new versions of the framework are released, I like to be able to switch between them easily.  I always make a soft link (symbolic link, symlink) to the latest release and name it Zend.  You can either right-click on the ZendFramework-1.10.3 folder and select “Make Link” (make sure to name the link “Zend”), or create a soft link from the command line like so:

~/phplib/ZendFramework-1.10.3$ ln -s ZendFramework-1.10.3 Zend

When a new version of the framework is released, I install the new version to its own folder and switch the soft link to target the directory containing the latest release.  That can come in handy down the road.

Get Zend Tool running

There are a few different ways to get Zend Tool running, perhaps better than mine, but I like to create a bash alias for zf.sh (For information on how to get bash aliases working, see this bash aliases tutorial.). My alias looks like alias zf='/usr/share/phplib/Zend/bin/zf.sh'.

Whichever method you use, make sure to test your alias by calling zf --help from the command line.

Creating a new Zend Framework Project

Now we’re getting to the good stuff.  First, head back to your home directory and create a new directory called public_html.  This is where you’re going to store your Zend Framework project (and any future web projects, for that matter).

Next, cd into the public_html directory and execute the following command:

~/public_html$ zf create project ZeroToZF

That’s all there is to it!  You’ve got your project structure in place, a default IndexController and ErrorController, your application config, the necessary view scripts, etc (See the Zend Application Quick Start for full rundown of what got created.).

IMPORTANT: While the full project structure is now in place, the Zend Framework is not included in your project for you.  Deciding how the framework should be included in the project is up to the developer.  I like to copy the library out of the Zend Framework install folder into the application’s library folder.  In our example, I would copy

/home/jkendall/phplib/ZendFramework-1.10.3/library/Zend

into

/home/jkendall/public_html/ZeroToZF/library

When done properly, the full path to Zend/Log.php in your application should be

/home/<username>/public_html/ZeroToZF/library/Zend/Log.php

Sweet!  We’re almost there.

Creating a Virtual Host

There are a decent number of steps here, but executing them is straightforward, so bear with me.  Also, a lot of these steps need to be executed from the command line.  Forewarned is forearmed and all that.

If you have not done so already, open the terminal application (Applications -> Accessories -> Terminal) and cd into /etc/apache2/sites-available.  Here you’ll find virtual host definitions.  Most likely you’ll see a file named default (or possibly 000-default).  Peek at that if you’d like to see an example of a virtual host, but what we’re going to put together is a lot simpler.

IMPORTANT: Since /etc and its subfolders are owned by root, you’ll need to run all of the following commands as root.  We’ll be using both the sudo and the gksudo commands to do that.

After using the command line to cd into /etc/apache2/sites-available, execute the following command to create your own vhost file:

/etc/apache2/sites-available$ gksudo gedit ZeroToZf.local

The file extension isn’t important, a lot of people use .conf, but I like to use .local for sites hosted locally.

Once you’ve got gedit open, the file’s contents should look like this:

<VirtualHost *:80>
        ServerName zerotozf.local
        DocumentRoot /home/jkendall/public_html/ZeroToZF/public
</VirtualHost>

Of course, jkendall should be replaced by your username.

Next we need to make your new virtual host file available to Apache.  You can do that by executing

/etc/apache2/sites-available$ sudo a2ensite ZeroToZF.local

If everything worked properly, you should see the following message:

Enabling site ZeroToZF.local.
Run '/etc/init.d/apache2 reload' to activate new configuration!

Next, edit your /etc/hosts file by adding zerotozf.local like so:

/etc/apache2/sites-available$ gksudo gedit /etc/hosts

Add the following line below the entry for localhost

127.0.0.1 zerotozf.local

NOTE: When adding a host to /etc/hosts, the case of the hostname is not important.  I like to add hostnames in lowercase, but you can add it as ZeroToZF.local if you like.

Save the file, close gedit, and execute

/etc/apache2/sites-available$ sudo /etc/init.d/apache2 reload

You should see the message

* Reloading web server config apache2                                          [ OK ]

Open up your browser and visit http://zerotozf.local.  You should see the Zend Framework welcome screen.

Congrats!  You did it!

Next Steps

Now that you’ve got your first project up and running, you’re probably going to want to actually do something with it.  I’d highly recommend heading over to Rob Allen’s site for his excellent Getting Started with Zend Framework tutorial.  It’s the same tutorial I followed when I first started with the framework (an earlier version, of course), and I’ve referred back to it many times since.

Wrapping Up

Getting started with the Zend Framework was a challenging proposition for me.  Just getting to the point where I could start working on a project was sometimes maddening as a result of all the steps involved, many of which had nothing to do with the framework, at least not directly.  I’ve tried, hopefully successfully, to lay out all the steps you might need to get from zero to a Zend Framework project in (about) 10 minutes.

Working with the Zend Framework has been a rich and rewarding experience for me.  I’ve learned almost everything I know about best practices, object oriented programming, *nix, and Apache (to name just a few) as a direct or indirect result of the Zend Framework.  I wouldn’t have been able to do that without the help of the Zend Framework community.  I won’t name names, as I’m sure to unintentionally leave out some great folks, so I’ll throw you a link to the Zend Framework Community Forum.  Head over there when you’ve got a Zend Framework problem you just can’t solve on your own.  You’ll meet some great folks and learn a lot in the process.  They’ve saved my bacon more than once.

If I’ve left out any steps or made some egregious error, please let me know in the comments.  I’ll be grateful and post updates and corrections as soon as possible.

Resources

14 thoughts on “From Zero to Zend Framework Project in 10 Minutes

  1. Pingback: jeremykendall.net » Blog Archive » From Zero to Zend Framework … Zero Me

  2. Pingback: Zend Framework News » Blog Archive » Von Null auf Zend Framework in 10 Minuten

  3. Pingback: Tweets that mention jeremykendall.net » Blog Archive » From Zero to Zend Framework Project in 10 Minutes -- Topsy.com

  4. Again, another nice post, Jeremy. ;-)

    But I’m mystified why people seem to view Zend_Tool as such a big deal. What’s so hard about having a ZF project skeleton stashed someplace and simply copying it into the folder for your project? Still have to set up the virtual host and the local hosts file.

    Create new controller/action? Copy/paste an empty controller and add corresponding view scripts as required. Easy.

    So, where’s the big benefit to Zend Tool?

    Thanks and cheers!

  5. Hey David,

    Thanks for the kudos and the feedback ;) It’s much appreciated.

    You’ve got a great point about keeping a project skeleton stashed away somewhere. I used to do that myself. Here are some of my arguments in favor of Zend_Tool.

    The biggest problem I have with project skeletons, and a reason I think Zend_Tool is such a big deal, is the need to keep the project skeleton up to date. If something in ZF changes (think Zend_Loader::registerAutoload() vs. Zend_Loader_Autoloader), I don’t have to go back into my skeleton and refactor.

    Using Zend_Tool takes less than a second (literally) to set up a full project structure. Copying and pasting your skeleton (unless you keep it in svn or git) will take longer. Automation FTW.

    Finally, you and I have no problem setting up a skeleton, refactoring for changes in the framework, setting up new vhosts, etc. We’ve been doing this for a while. Consider the new guy, however. Do you remember what a PITA it was getting a project skeleton together the first time around? Zend_Tool takes away all of the grief you and I had to go through to get our first few projects up and running. I think that’s a really good thing.

    Thanks!

  6. Hi Jeremy,

    Your point about keeping a skeleton up-to-date is well-taken. I hadn’t considered that.

    Also, I am seeing that there is more to Zend_Tool than just setting up a boilerplate project. There is a new series on Zendcasts released just yesterday that goes into some of what Zend_Tool can do – creating your own providers, etc. There is obviously a lot more there than I understand. So far. ;-)

    The recent Zendcast – first in a two-part series – is at:

    http://www.zendcasts.com/integrating-zend_tool-into-your-application/2010/04/

    Looking forward to the second installment. And to more good stuff from you. ;-)

    Cheers,

  7. Now that I am using VirtualBox with an Ubuntu VM, I got a chance to actually use your instructions.

    Awesome. Just awesome.I, too, am now a fan of Zend_Tool. Like you said, literally a second to set up an app (once you have your ZF lib and bin set up). A bit of virtual host config – and with symlinks available, it’s so much cooler on Linux than on Windows – and we’re good to go.

    One note: You keep the various Zend libraries in your home directory (/home/jkendall/phplib) and create a symlink for the “active” one. Cool. But you claim your zf script alias points to:

    /usr/share/phplib/Zend/bin/zf.sh

    I assume that was a typo? I pointed my alias to:

    /home//phplib/Zend/bin/zf.sh

    and all was cool. Easier than copying the bin into into /usr/share/phplib. But still, six one way, half-dozen the other.

    Anyway, thanks for a very clear and helpful post. Cheers!

  8. Pingback: rhymes with milk

  9. Pingback: Cgi script installation service

  10. After examine a few of the weblog posts in your web site now, and I really like your manner of blogging. I bookmarked it to my bookmark web site listing and will likely be checking back soon. Pls check out my website online as properly and let me know what you think.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">