If you are working with PHP often you’ve surely stumbled on the term “Zend” once or twice.
Using the Zend Framework offers a lot of flexibility and speed for rolling out fast project files once you get good at using the class libraries and knowing how to plug-in and extend the core code base.
I was working with one of our new interns today and getting them setup with the “kitchen sink” as we call it.
First, Install MAMP. For those of you that haven’t used it Mamp is a nice bundle of Apache, MySQL, and PHP for your Mac. Its an easy install and very easy to turn on and off for those times you don’t need it. You can certainly use the built in web sharing features on your mac if you choose but personally i’ve always liked to use MAMP as its easier to manage and doesn’t require as much customization to really get your environment setup quickly for novice users.
- Visit the Download Page
- Download the Mamp DMG install file (named MAMP_MAMP_PRO_1.9.2.dmg)
- Double click and open the Mamp DMG – Drag the Mamp folder to the Applications folder as seen in the install graphic.
- Open your Applications Folder and look inside the MAMP folder to find the MAMP.app file fire that open
- Click on the Start Servers button if you haven’t already and you should see the “green” lights for Apache Server and MySQL Server. This lets you know things are running locally. To stop running Apache and MySQL simply click “stop servers” and quit the application.
- Clicking on “Open start page” will display your dashboard and will output an easy screen for seeing your MySQL information, a link to php info and the other links to phpMyAdmin, along with the other built in features. we told you it was a nice bundle didn’t we?
- Your Document Root folder is located in /Applications/MAMP/htdocs so any scripts you have running inside of that folder can be called via localhost. By Default MAMP operates on port 8888 so you will need to prefix your URL with that link.
On to the Zend setup:
- Wow your either pretty fast or already had MAMP up and running. In order to start leveraging the power of Zend you will need to download and install the framework.
- Zend often updates the framework with new class libraries, security updates and other fruitful updates so rather then consistently re-downloading it its sometimes easier to just grab the latest stable build via SVN. To do so:
Fire open Terminal App and issue the following commands.
For every line break you have to hit the RETURN key (You would be surprised how many times I’ve had to remind people of this)
cd /Applications/MAMP/ mkdir svn cd svn mkdir zendframework cd zendframework svn checkout http://framework.zend.com/svn/framework/standard/trunk
After you hit return on the last line you will be greeted with what looks like the matrix while your terminal application downloads the latest files and places them inside of your zend framework folder. “cd” means change directory, “mkdir” means make directory.
Once the download has completed look inside of /Applications/MAMP/conf/php5/php.ini
You can open your php.ini file with any text editor or IDE of your choice.
Once you have the php.ini file open look for a line include_path and add the path to the new install of zend.
include_path is a declaration and tells PHP the various settings for different things inside php. If you are using a shared web server or haven’t messed with PHP.ini before its pretty much the file that contains all your settings.
Similar to setting variables in php itself you will note each directive is wrapped inside of double quotes ” ” with the path in this case.
By default my php.ini reads:
include_path = “.:/usr/lib/php:/usr/local/lib/php”
By adding a : after the last location I can set multiple locations for includes in my php environment. This is a Unix thing and can be used in any declaration of a config value inside of Unix/Linux.
So I have added the path to my new install of Zend Framework as seen below.
include_path = “.:/usr/lib/php:/usr/local/lib/php:/Applications/MAMP/svn/zendframework/trunk/library”
Save and close your php.ini file. Had we not added this include path PHP wouldn’t be able to locate the core files for zend for us building our application.
Setting up your First Zend Project:
Now that all the heavy lifting is done lets create a folder for our new project. In this case we are creating DS-firstproj. Open up /Applications/MAMP/htdocs/ and create a folder called DS-firstproj. You can do this via finder as you would normally create a folder or test our your new “cd” change directory and make directory “mkdir” skills via terminal.
If you have ever used frameworks before most of them have built in tools for setting up projects and scaffolds. Other popular open source frameworks include: like Ruby on Rails, Kohana (PHP), Cake Php (PHP), Django (python) to name a few.
Inside of /Applications/MAMP/svn/zendframework/trunk/bin/ you will find a shell script titled zf.sh
Think of shell scripts like windows .exe (executable) files that can be run as scripts via terminal.
Open up terminal if you don’t have it open still and navigate to the /Applications/MAMP/svn/zendframework/trunk/bin/ folder.
From here you can see the contents of the directory by issuing a “list” command via terminal type the command and remember to hit return as we discussed above
As seen above you now see the contents of that folder are zf.bat, zf.php, and zf.sh
We want to run the Zend Framework tool and can do so by calling the zf.sh script.
In Unix based operating systems you can call scripts by adding a ./ before the script.
Call the Zend framework script zf.sh like this:
After hitting return you will notice the script is called:
Don’t worry if your colors are different and pay no attention to the error in red. the zend framework tool is looking for us to issue it some commands hence it throwing back an error.
Now if you are going to be using the tool quite a bit you aren’t going to want to always have to navigate to that folder to run the script. To prevent you from doing that we are going to create an Alias
An alias, is like a desktop shortcut and just tells your version of Linux/Unix where to locate the files. Type the following:
The alias “zf” has now been created. If you close and reopen terminal however this will not store as you need to create this inside of your Bash Profile. I’ll write a post on the Bash Profile for next time for now lets get back to creating the first test project.
From that same terminal window now issue the following command:
zf create project DS-firstproj
As seen in the above screenshot the zend tool runs the zend framework script zf.sh and by issuing it the create project command following by the string name of the project will go and create a base project containing all the prepacked files needed to create a zend based web application.
Since our script ran it created files outside of our htdocs directory so we will need to copy files.
Navigate to /Applications/MAMP/svn/zendframework/trunk/bin and you will find the DS-firstproj folder we have created using the script above.
Copy the folder and navigate to /Applications/MAMP/htdocs where you will paste the folder and its contents.
Now open your browser and copy and paste this into the location bar: http://localhost:8888/DS-firstproj/
This will open the base folder.
Inside of this folder (view it from finder as in the browser will purposely hide things for security reasons)
At this point if you aren’t already familiar with MVC (Model View Controller) or other design patterns for programming things will get pretty hard to discern quickly but i’ll give a quick overview to the high points. Certainly read the below link to Zend where they walk through the specifics of a default project.
The Application folder houses the core MVC system. This is the guts of your application and inside it is the bulk of your code and files.
Configs hold the bulk of the configuration files in the application. Similar to editing the php.ini all initialization files typically reside in this folder.
Models – The M in MVC are stored within here.
Views – The V in MVC are stored within here. This is the presentation layer of your application and where Markup and client-side code is ran.
Controllers/helpers – The C in MVC are stored within here. This is responsible for handling the business logic of your application reside here.
Bootstrap.php – this is the “starter” for your application and is responsible for firing things up.
By default the Index page to your project is found at: /application/views/scripts/index/index.phtml
If you are following along with this tutorial you can just click this link via mamp
For Additional information on the Zend Framework read the programmers reference guide
Looking to get a taste of MVC and web development with php but having some trouble with Zend? I typically like to show people new to PHP or framework development Code Igniter. Code Igniter is a php framework by Ellis Labs, the creators of the popular Content management system Expression Engine.