Zend Framework quickstart – Part 1

Here are the links that I followed while getting started with the Zend Framework:

So far, Zend Framework looks pretty much like CodeIgniter in a way that I can use loosely-coupled component libraries at will and I have a lot of control over the architecture. It also provides an MVC implementation similar to CodeIgniter.

When I was reading the links above, I noticed that some of the details were missing in order to successfully complete a small project using Zend Framework without pulling my hair a couple of times. So I wrote my own so I can just look this post up if ever I forget some of the important details and perhaps some other ZF n00b out there can benefit from this post as well. Here it is…

Get a copy of the Zend Framework

I downloaded the latest version of Zend Framework from the Latest Release of ZF page instead of installing Zend Server (which contains Zend Framework with a complete PHP stack) on my development machine since I don’t have a production server where I can install the Zend Server, because Zend Server will replace your Linux distro’s PHP that may cause conflict between RPM packages. I’d rather setup my development environment similar to the setup of the servers that I have right now so don’t have to worry much about package incompatibilities during deployment.

In the Latest Release of ZF page, choose the full framework which doesn’t require registration. In my case, I downloaded Zend Framework 1.10.8 Full (released 2010-08-25). Extract the contents of the archive. In my (Fedora) Linux machine, I extracted it under /home/randell/ZendFramework-1.10.8. In Windows, I extracted it under C:/ZendFramework-1.10.8.

Setup the zf command line tool

We’ll be using the zf command line tool to create our new project. For that, we need to properly setup our environment variables for both PHP and the zf tool. The zf command line tools (zf.bat for Windows and zf.sh for Unix-based OSes) are located in the /bin directory of your Zend Framework installation.

Create the project

After setting-up the command line tool, we can now create our project. From the command line, navigate to the directory where we will create your project. In my Linux machine, I created it under /var/www/html/. In Windows, I created it under C:/xampp-1.7.1/htdocs. Both directories is where I run all my other PHP projects. To create the project, execute the following command:

zf create project quickstart

Running this command will create your basic site structure, including your initial controllers and views. Both in Windows and in Linux, you could get a message similar to this after executing the command:

Note: This command created a web project, for more information setting up your VHOST, please see docs/README

The tree structure of the generated project looks something like this:

quickstart
|-- application
|    |-- configs
|    |    `-- application.ini
|    |-- controllers
|    |    |-- ErrorController.php
|    |    `-- IndexController.php
|    |-- models
|    |-- views
|    |    |-- helpers
|    |    |-- scripts
|    |         |-- error
|    |         |    `-- error.phtml
|    |         |-- index
|    |              `-- index.phtml
|    `-- Bootstrap.php
|-- docs
|    `-- README.txt
|-- library
|-- public
|    |-- .htaccess
|    `-- index.php
|-- tests
|    |-- application
|    |    `-- bootstrap.php
|    |-- library
|    |    `-- bootstrap.php
|    `-- phpunit.xml
`-- .zfproject.xml

Don’t worry about the contents of those files yet because I will try to explain the important ones as detailed as possible in my next post. For now, let’s just get the basic setup up and running.

Next step is to copy the Zend library (library/Zend)from our extracted Zend Framework archive to the library directory of our project. We’re doing this so that our project has a copy of the Zend Framework whenever we decide to deploy it on a different server.

Create a virtual host

Let’s modify our httpd.conf and hosts files using any text editor.

Append the following lines at the end of httpd.conf file (/etc/httpd/conf/httpd.conf and /etc/hosts in Fedora):

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName quickstart.local
    DocumentRoot /var/www/html/quickstart/public

    SetEnv APPLICATION_ENV "development"

    <Directory /var/www/html/quickstart/public>
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
<VirtualHost>

DocumentRoot specifies the public subdirectory of our project (only files under that directory can ever be served directly by the server). Take note of the public. Most people tend to forget that part, especially when coming from other frameworks where the index.php doesn’t reside on a directory other than the root. The SetEnv directive sets an environment variable for our virtual host and will be picked up by index.php (in the public directory) and set the APPLICATION_ENV constant for our application.

Append the following to the hosts file (/etc/hosts in Fedora and C:/WINDOWS/system32/drivers/etc/hosts in Windows XP):

127.0.0.1 quickstart.local

Then restart our server. We can now access our application from http://quickstart.local and see something like this from our browser:

Welcome to the Zend Framework

Welcome to the Zend Framework

Got questions or suggestions about this post? Feel free to post a comment or drop me an email.

Setup Zend Command Line Console Tool in Windows XP

The CLI, or command line tool (internally known as the console tool), is currently the primary interface for dispatching Zend_Tool requests. With the CLI tool, developers can issue tooling requests inside the “command line windows”, also commonly known as a “terminal” window.

Some examples of actions that you can perform with the Zend Framework Command Line Console Tool (a.k.a Zend CLI Tool) are the following:

zf create project path-to-project name-of-profile file-of-profile
zf create model name module
zf create view controller-name action-name-or-simple-name

Assuming that we have downloaded and extracted the Zend Framework archive, Running it on Windows by typing zf.bat in the command prompt under the /bin directory without setting-up the environment variables will give us the following message:

‘”php.exe”‘ is not recognized as an internal or external command, operable program or batch file.

In order to let Windows know how to execute PHP scripts, we need to tell it where php.exe is located. To do that, right click on My Computer, click Properties, click on the Advanced tab, and click the Environment Variables button. Under System Variables, select Path and click on the Edit button. At the end of the Variable value field, add the path to your php. In my case, I appended

;C:xampp-1.7.1php

The semicolon is used to separate the paths  in the Path System Variable value field. To test it we can type the following from the command prompt under the our zf.bat‘s directory:

zf show version

and we should get a message similar to this:

Zend Framework Version: 1.10.8

We can also just execute zf to check out the other actions you can perform with the Zend Tool.

Now we also want the Zend command line tool to be reachable from our system path so that we can call zf from the command prompt, regardless of our current work directory. Similar to how we added php.exe in the environment variables, let’s add the path to our zf.bat to our path environment variable. In my case, I added ;C:ZendFramework-1.10.8bin. To test it, execute zf show version from your command prompt in any directory and you should get the Zend Framework version similar to the one we got earlier.

Setup Zend Command Line Console Tool in Fedora Linux

The CLI, or command line tool (internally known as the console tool), is currently the primary interface for dispatching Zend_Tool requests. With the CLI tool, developers can issue tooling requests inside the “command line windows”, also commonly known as a “terminal” window.

Some examples of actions that you can perform with the Zend Framework Command Line Console Tool (a.k.a. Zend CLI Tool) are the following:

zf create project path-to-project name-of-profile file-of-profile
zf create model name module
zf create view controller-name action-name-or-simple-name

Assuming that we have already downloaded and extracted the Zend Framework archive, we would want to be able to issue the zf command from the command line from any directory. It means we need to make <path/to/zend>/bin/zf.sh reachable from our system path, wherein zf.sh is the *nix-specific wrapper. This wrapper is responsible for finding the proper php.exe and zf.phpzf.php in turn is the responsible for handling understanding our environment, constructing the proper include_path, and passing what is provided on the command line to the proper library component for dispatching.

Issuing the command

echo $PATH

tells me that /home/randell/bin is part of the PATH environment variable, so we simply need to create a link from that location to zf.sh.

To do that, issue a command similar to this (replacing my username with your username, of course):

ln -s /home/randell/lib/ZendFramework-1.10.8/bin/zf.sh /home/randell/bin/zf

To test it, execute the following command:

zf show version

It should print a message similar to

Zend Framework Version: 1.10.8

We can also just execute zf to check out the other actions you can perform with the Zend Tool.

PHP Fatal error: Class ‘DOMDocument’ not found

In case you encounter an error similar to this:

PHP Fatal error:  Class 'DOMDocument' not found in /home/randell/lib/ZendFramework-1.10.8/library/Zend/Tool/Project/Profile/FileParser/Xml.php on line 82

wherein line 82 contains

$doc = new DOMDocument('1.0');

it probably means that you tried to create a DOMDocument while your PHP installation doesn’t have support for manipulating XML Documents using the DOM tree, or performing XSL transformations on XML documents.

In Fedora, you can solve this by installing the XML module for PHP by executing the following command as root:

yum install php-xml

Warning: date(): It is not safe to rely on the system’s timezone settings.

Warning: date(): It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.

If you are getting this error in any of your PHP projects, the fix would be to add the date.timezone directive in your php.ini like this:

date.timezone = "Asia/Manila"

or call

date_default_timezone_set('Asia/Manila');

before calling any date or time functions. Of course, you can set it to other supported timezones. You might need to restart your web server for the changes to take effect if you choose the former.

The date.timezone directive existed prior to PHP 5.3.0, but was generally commented out that’s why it probably messed up your projects after updating your PHP versions to 5.3 (and mine too!)

Setting-up Fedora 11

Fedora 11 was released a month ago, but it’s only now that I am able to completely configure my desktop to run on top of it.  It is because I’m still happy with Fedora 10.  I’ve established my development environment and I find no need to upgrade it yet.  In any case, I’m going to upgrade my Fedora 10 installation once I’m able to fully clone my environment in my other machine.

Similar to how I configured my Fedora 10 installation, I selected both Web Development and Web Server options when asked what additional tasks I want my system to support, to make my post-installation setup easier.

I also selected the following packages during installation:

Desktop Environment

  • KDE (K Desktop Environment)

Development

  • Fedora Eclipse
  • Java Development

Servers

  • MySQL Database
  • PostgreSQL Database

And here are my post-installation configurations:

Continue reading

Podcasts

I’ve been listening to podcasts lately using my trusty PSP.  I realized I needed to make better use of my 4 hours of daily travel to and from the office aside from playing games, reading novels, listening to audiobooks, or sleeping.

Here’s my list:

  • Pinoy Tech Podcast – hosted by Jerome Gotangco with Dean Berris, Jon Limjap, Rom Feria and Migs Paraz. Some of the topics they’ve discussed include micro-blogging, open source desktop adoption in the country, Google Maps, the EHeads super concert, and the history of the internet in the country.  So far, they’ve had 4 episodes.
  • PHP Abstract – for PHP Developers by PHP Developers.  It seems to me that they stopped at the 40th episode because the the last release date was 05/15/2008.

I know, 2 isn’t really a list.  And right now, I’m at the 14th episode of PHP Abstract and I’m expecting to consume the rest of the episodes before the end of the week.  What podcasts do you recommend?

Setting-up Fedora 10

The last time I setup my Fedora 9 installation, I did not select the Software Development and Web Server options when I was asked what additional tasks I would like my system to support for.  This time, in Fedora 10, I checked them both.  I wanted to see if post-installation setups will be easier.  I also wanted to check out more applications.

I selected the following packages during installation:

Desktop Environment

  • KDE (K Desktop Environment)

Development

  • Fedora Eclipse – based on Eclipse 3.4.1
  • Java Development – OpenJDK Development Environment
  • Web Development

Servers

  • MySQL Database
  • PostgreSQL Database
  • Web Server
    • php-pgsql-5.2.6-5.x86_64

And here are the post-installation setups:

Continue reading