Getting Started With Zend Tool

10th August 2009

The latest versions of Zend Framework come with a handy little tool that gets you up and running with a basic Zend Framework install within a few moments. When you have downloaded Zend Framework you will notice that in minimal and full there is a directory called bin. This seems to be intended to contain lots of different tools, but at the moment it contains three files that are part of Zend Tool.

The important file in this directory is zf.php which is the actual core of the tool. The other two files are used depending on what operating system you are running. If you are running Windows then it is zf.bat that will be used, otherwise it is zf.sh that is important.

To run Zend Tool you will need to add the PHP executable to your system path, if you don't do this then your system will not know what to do with the file and will error. On Windows, if your PHP directory is located at c:\php then add this to your PATH variable, separated from other values by a semicolon. On UNIX/Linux systems you will need to use a command like the following:

  1. PATH=$PATH:/path/to/php
  2. export PATH

If you want to test that Zend Tool is up and running just open a command prompt and navigate to the bin directory in your Zend Framework directory. Run the following command to see the version of Zend Framework that this tool belongs to.

zf show version

On Linux you might have to run this as ../zf.sh, it depends on what system you are on. This will output something like:

Zend Framework Version: 1.9.0

If you see this then Zend Tool is ready to go. To see the sorts of commands available with Zend Tool type zf and hit enter. This will cause the tool to error, but it will show a list of available commands that you can use with the tool. We will now run through a few of them.

Create Projects

The first thing you will want to do with the tool is to create a project. You do this by using the following command, entering whatever you want as the project name.

zf create project [project name here]

You can also just enter just zf create project and the tool will prompt you for a project name.

This will create a directory in your bin directory that is called whatever you decided the project name to be. This single step actually creates a lot of the functionality that takes the time when setting up a project. All of the needed directories like application (with all of the standard subdirectories), library and public are present, but the tool also creates a Index controller and an Error controller, along with all of the needed views. The public directory contains a index.php file and a .htaccess file. An application.ini config file and a Bootstrap.php file are also created and have a minimal amount of content.

Additionally, if you are into testing your Zend Framework applications then you will notice that there is also a tests directory. I won't go into the contents of this directory here, but this is where you start if you want to create unit tests for your application.

What has been created is a fully functioning project. You could drop this into a web directory and it would work straight away, showing you a nice little HTML page. One thing you should be aware of is that the tool does not copy the framework into the library directory. You will have to do this yourself when you set up your project.

Those of you who are used to Zend Framework 1.7 will notice that there are some very minor changes to the system. There has been some standardization work on how Zend Framework applications will fit together and a result of this is a new Bootstrap class, some new options in the ini file and standard index.php and .htaccess files. These changes are minor, but are worth looking into if you haven't seem them before.

Create Controllers

To do anything with this created project you need to be inside it. Part of the creation of the project involves the creation of a file called .zfproject.xml. This is used by Zend Tool to see what has been created and how it has been created and needs to be in the current working directory for the tool to work. The next thing we are going to do is create a controller for accessing user functions, we will call this controller User. To create the User controller change to the directory of the project you created and call Zend Tool in the following way for Windows.

..\zf create controller user

Linux users will need to use the following:

.././zf.sh create controller user

Notice the capitalisation in both cases. This will create a controller called User, with a single index action, along with a view for that action in the views/scripts directory. You can opt not to include the index action within this controller by adding a 0 to the end of the above script, the default value is 1, which creates the index action.

Create Actions

To create an action in a controller use the following command:

..\zf create action [action name] [controller name]

This will create an action (specified in the action name argument) in the controller (specified by the controller name argument) along with the views for that action. If you leave out the controller name then the Index controller will be used when creating the action. You can also tell Zend Tool not to make the view for this action by adding a 0 to the end of this argument, the default value is 1, which creates the view.

Create Views

To create a view using Zend Tool use the following command:

..\zf create view [controller name] [action name]

Every time you create a view (including when you are creating actions, controllers and projects) Zend Tool will add some text stating where the view is being called from. As an example, the following text will be seen when viewing the text action of the Index controller.

View script for controller index and script/action name test

This can help with development and testing, especially if you are new to Zend Framework.

Create Modules

To create a module using Zend Tool type the following command:

..\zf create module [module name]

This will create a directory in you application directory (if it doesn't already exist) called whatever you entered for the module name. This inner directory will contain all of the needed directories (modules, controllers etc) but they will contain no files. You will therefore need to create a controller within that module. To create a controller within this module use the following command:

..\zf create controller [controller name] 1 [module name]

The controller name is the name of the controller to be created and the module name is the name of the module that you created in the previous step. The 1 tells Zend Tool to create an index action inside this controller. A view will also be created in the scripts directory for this controller. The commands for creating actions will work in a similar way, you just need to append the module name to the end of the command.

..\zf create action [action name] [controller name] 1 [module name]

Remember that the number 1 here will create a view for this action, this is the default value and can be overridden with a 0.

Note that although you have added a module your Zend Framework will not know about it. You will need to code in the needed commands to enable your application to use modules!

Finally, you should be aware that Zend Tool in Zend Framework 1.9.0 seems to have a bug with Zend Tool that causes a couple of errors to appear when the tool is run. Apparently this has been fixed for the next release, but you won't be able to do anything other than creating the project. Creating controllers, actions and views just errors and does nothing. The Zend Tool from Zend Framework 1.8.x works fine and creates forward compatible projects.

Comments

Permalink
Hi, I followed the steps to create my project and it works perfectly, but when I try to create a module, it shows me a message "An error ocurred, the project was not found". What is happening, I have the .zfproject.xml file and when I create the module I'm inside the project. Please help me thanks

Gabriela (Sat, 10/31/2009 - 22:16)

Permalink
It sounds like you didn't change the directory once the project was complete, although I could be wrong. Once you have created the project you need to change into that directory and work from there.

philipnorton42 (Sun, 11/01/2009 - 21:34)

Add new comment

The content of this field is kept private and will not be shown publicly.