I’ve had a couple of cases where I’ve wanted to quickly test some stuff with the Zend Framework. Now, to get the framework up and running, you’ll need a couple of things:
- Directory structure – controller dirs, view dirs, public_html, etc. etc.
- .htaccess files – To enable routing
- Bootstrap
- Controllers
I can never remember what to put in the .htaccess files and I often forget the parameters for the front controller in the bootstrap. But what if you could do all these steps with a single command?
I want to be able to run a single command and get the framework up and running without doing anything else.
So, I decided to whip up my trusty Vim and write a couple of shell scripts to do it!
“But why should I use scripts to do this? I mean it takes only a minute to do it by hand!”
Maybe, but it’s annoying! I don’t find creating directories and other stuff interesting – maybe someone who isn’t very experienced with computers finds it amusing, but I sure don’t. I want to get coding, and this is a step that’s preventing me from doing what I want to do and forces me to spend time doing something else… so rather than spending a minute at it, I’ll spend 30 minutes writing and testing some scripts which will do it in 1 second. ;)
What do the scripts do?
I wrote three script files:
- zf-controller.sh – Creates a basic controller skeleton in a file you specify
- zf-module.sh – Creates the directory tree for a module: name/controllers, name/models, name/views, name/views/scripts etc.
- zf.sh – Does everything in one shot. This creates a complete Zend Framework modular dir tree including the default module, an index controller for it, an empty view script, a public_html dir, .htaccess files and a bootstrap. In other words, your one shot framework initialization script.
With these scripts, it’s very easy (and quick!) to get the framework up and running.
Where do I get them?
You can download the zfinit scripts here (tar archive)
How do I use them?
Simple. First, untar the above archive file to a directory. Then, you can configure the scripts.
You can edit the zfinit.index.php file to customize the bootstrap the script creates and if you’re using a custom base controller class other than Zend_Controller_Action, you can open up zf-controller.sh and change the BASE_CONTROLLER variable. All of this is optional, though.
The parameters for the scripts are as follows:
zf.sh [path]
path is optional, it specifies the directory where you want to initialize the framework. Note that it must exist. If not specified, the script will use the current directory.
zf-module.sh [path]
Will create a module directory tree to path. Optional, if not specified, the dir tree will be created to the current directory.
zf-controller.sh name is the name of the controller, for example IndexController. Module is the name of the module this controller belongs to. Remember that the first letter in the module name is upper case, ie. Default. Path is again optional and defines to what dir the file will be created. Create the basic framework, an admin module and an indexcontroller for the admin module: Nice and easy. :) Note that the scripts don’t necessarily have to be in the same directory as you’re working in. You can have them in some other directory as well, as long as all of the files from the tar file are in the same one.Example usage
./zf.sh
./zf-module.sh application/admin
./zf-controller.sh IndexController Admin application/admin/controllers