Video icon 64
Learning to code? Skill up faster with our practical video courses. Start your free trial today.
Advertisement

Configuring Zend Framework to work with a Local Development Environment

by
Student iconAre you a student? Get a yearly Tuts+ subscription for $45 →

In this tutorial, we will go over how to install and configure the Zend Framework to work with your local development environment. I will be using Wampserver 2.0i on a Windows Vista computer but these steps can be adapted to XAMPP, MAMP, or whatever stack of Apache, PHP, and MySQL that you're using.


Local Development Environment

The first step of course is to ensure that your local development environment whether Wampserver, MAMP, or XAMPP etc. is installed, set up and running and that you can load your localhost with no problems. I assume that you already have this part done since this tutorial is not intended to cover setting up the environment.



Download the Zend Framework

The Zend Framework can be downloaded at http://framework.zend.com. You can download the full community server which includes an Apache, MySQL, and PHP stack and the Zend Framework itself and is already configured for you. But if you already have your own development environment all set up you probably don't want to do this, which is the point of this tutorial. We only want the Zend Framework. You can select the Full Package or the Minimal Package depending on your needs. If you don't want to register on the site there are direct download links available at the bottom of the downloads page.

Once you've downloaded the archived file, extract it and place the folder in a location of your choice. I generally prefer to rename the extracted folder and place it in the bin folder of my wamp folder. I have renamed the folder zf and placed it in my bin folder. Wherever you choose to place it, make sure you remember this location as it will be important.


Configuring the Zend Tool

The next step is to configure the Zend Tool. Since v1.8, the Zend Framework now comes with a command line tool that eases the job of creating and bootstrapping Zend framework applications. Instead of manually setting up your project, controllers, models, etc. and trying to make sure everything is in the correct folder, the Zend tool automates this process and is very convenient to use.

To use Zend tool, you need to be able to access the ..zf\bin\zf.bat file (or in the case of *nix systems zf.sh) from the command line. One way to do this is to type the complete path to the file and then the zf command. However, this is awkward and inconvenient. Instead, it's much better to add the file to your path. In Windows, we do this by opening the environment variables and adding the path to zf.bat to our PATH variable. You also need to make sure that php.exe is also in your path. How you do this will vary depending on your version of Windows, and if you're on a different OS then this process will vary too. The important thing to remember is that on Windows, once you change environment variables, you have to restart your computer for them to take effect. In my case on Windows Vista, I simply added C:\wamp\bin\zf\bin\zf.bat and c:\wamp\bin\php\php5.2.11 to my path variable.



Full Screencast



Create a New Project Using the Zend Tool

Now that we have the Zend tool set up and configured, it's time to test the Zend tool. Go to the command line (or shell) and type "zf show version". If Zend tool is correctly configured, you should see a response such as Zend Framework Version 1.9.6. You can now proceed to create a new project. Go to the command line and change directory to where you want to have your new project sit. In my case, I navigate to C:\wamp\www which is where I create all my projects.

If you simply type zf you'll see a list of all the things you can do with the Zend tool. To create a new project named myzfproject, simply type "zf create project myzfproject". After a few minutes, you will see a message that indicates the project has been created. If you navigate to the project, you can see your new project folder with all the necessary folders sitting where they need to be.



Including the Zend Library in your Project

When a new project is created using the Zend tool, the library folder in the project is empty by default. For your project to run, it needs to see the Zend Library and there are two main ways to make this happen:

  1. Include the path to the library which is already in your zf folder (C:\wamp\bin\zf\library) to your php.ini file.
  2. Place a copy of the Zend library into the library folder of each project you create

Each method has its own advantages and disadvantages and different developers have their own preference. My personal preference is to place a copy of the library in each project I create as this ensures that I always maintain version separation for my projects. It does however lead to having many copies of the library on my server which can seem redundant.

With the Zend tool configured and working, creating models, controllers, etc. is simply a matter of going to the command line and typing the appropriate command, e.g. "zf create controller controller-name", "zf create model model-name", and so on.

If you simply type zf you'll see a list of all the things you can do with the Zend tool. To create a new project named myzfproject, simply type "zf create project myzfproject". After a few minutes, you will see a message that indicates the project has been created. If you navigate to the project, you can see your new project folder with all the necessary folders sitting where they need to be.


If you now navigate in your browser to http://localhost/myzfproject/public (or wherever your project is set up) you should see the Zend welcome page.



Final Thoughts

  • rewrite_module: The front facing part of your application will sit in the public folder of your project. For this to be accessible, the rewrite_module must be enabled in your apache server. This is not enabled by default when you install Wampserver, so make sure you enable it.
  • Virtual Hosts: It's generally a good idea to create a virtual host for your project early on and point it to the public folder.
  • Error Creating Controller:: With version 1.9+ of the Zend Framework, you may need to uncomment one line in your application configuration file if you run into problems creating controllers. You may get a "fatal error cannot redeclare class Zend_Loader...". I generally have not had this issue with 1.8 but I've run into it more than once (and so have others in the Zend forums) in version 1.9. This happens because there seems to be a duplicate reference to the library in the configuration. You can see that there seems to be an issue redeclaring something, which would imply the thing is declared already. The solution to this is found by looking in the config file. Open the application.ini file in the project config folder (in my case C:\wamp\www\myzfproject\application\configs\application.ini) and comment out this line:


    includePaths.library = APPLICATION_PATH "/../library"

    by placing a semi-colon before it, thus:

    ;includePaths.library = APPLICATION_PATH "/../library"


    You should now not get this error when you try to create controllers, views, or models for your project.

If you have completed all the steps successfully, you should now be able to quickly set up projects using the Zend Tool in your local development environment without needing to install the Zend server.

I hope that this tutorial has been useful and will hopefully get you on your way to setting the Zend framework on your local development environment so that you can start exploring this powerful framework and learning how to work with it to develop your PHP applications.

I would be interested in knowing what differences there are in setting this up in different environments, such as what adaptions you had to make to these instructions on XAMPP, MAMP, or whatever your local development environment is, so please feel free to leave comments about your particular setup of configuring the Zend framework and the Zend tool.

Advertisement