Advertisement

Configuring Zend Framework to work with a Local Development Environment

by

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
Related Posts
  • Code
    Web Development
    Rapid Website Deployment With Django, Heroku & New RelicNew relic wide retina preview
    Rapidly deploying websites has never been easier, or as affordable as it is now, when combining Django, Heroku, and New Relic. Follow along in this tutorial as we build a portfolio website using these three excellent tools. Read More…
  • Web Design
    CodeKit
    Quick Tip: Use CodeKit 2.0 for Local WordPress DevelopmentCodekit thumb
    In this quick article, I'm going to explain how to use Codekit 2.0 and speed up your local development workflow with style injection, browser refreshing, and device testing without touching a line of code to setup!Read More…
  • Code
    WordPress
    An Introduction To Deploying WordPress with MinaImage400
    As a PHP application, WordPress is usually deployed by a very old method: uploading files via FTP. We have some deployment tools, but they often requires some type of Ruby skill. For example, one popular, powerful tool is Capistrano, but it's also very heavy with many Ruby/Rails related features. I also think that it's little bit tricky to install Capistrano for a PHP developer without any Ruby knowledge. So what options do we have as WordPress developers? In this tutorial, I will introduce you Mina: A small, light tool aims to fast deployment and server automation.Read More…
  • Code
    PHP
    Setting Up a Local Mirror for Composer Packages With SatisComposer retina preview
    Installing all your PHP libraries with Composer is a great way to save time. But larger projects automatically tested and run at each commit to your software version control (SVC) system will take a long time to install all the required packages from the Internet. You want to run your tests as soon as possible through your continuous integration (CI) system so that you have fast feedback and quick reactions on failure. In this tutorial we will set up a local mirror to proxy all your packages required in your project's composer.json file. This will make our CI work much faster, install the packages over the local network or even hosted on the same machine, and make sure we have the specific versions of the packages always available.Read More…
  • Code
    Resources
    Recommended Resources: WordPress DevelopmentCode
    We utilize numerous tools and services on Tuts+, as well as teach how to use them through regularly published educational material on this site. Here are our top recommended resources for WordPress development professionals, which is put together by our editorial staff.Read More…
  • Code
    Web Development
    Introduction to Sails.jsSails preview 400 2
    Sails is a Javascript framework designed to resemble the MVC architecture from frameworks like Ruby on Rails. It makes the process of building Node.js apps easier, especially APIs, single page apps and realtime features, like chat.Read More…