Two Ways to Register Libraries with the Zend Autoloader

Follow these two steps to make a PHP class library available via auto loading in your Zend Framework application:

  1. Move or symlink the library into your application’s library directory (/my_project/library). For example, I have a personal PHP library that I keep in /usr/local/lib/MM. I create a symlink in the library directory:
    1. cd /var/www/my_project/library
    2. ln -s /usr/local/lib/MM
  2. Now you need to register your library’s namespace (class prefix). There are two files that you can do this in:
    1. The Bootstrap File:
      1. Add the following method to your application’s bootstrap class file (/my_project/application/Bootstrap.php), changing MM_to your own library’s prefix:
        public function _initAutoloader()
        {
            Zend_Loader_Autoloader::getInstance()->registerNamespace('MM_');
        }
    2. The application.ini File
      1. Add the following line to /my_project/application/configs/application.ini:
        1. autoloaderNamespaces[] = "MM_"

I have found that, most of the time, if something can be configured in a ZF application’s Bootstrap class, that it can also be done in the application.ini file. I prefer pushing as much configuration into the application.ini file as possible, as it is usually much clearer than it is in the bootstrap. I try to keep the bootstrap as “slim” as possible.

Installing PHPUnit on CentOS 6.0

These are the steps I took to install PHPUnit on CentOS 6.0.

  1. Upgrade PEAR:
    1. pear upgrade PEAR
    2. This will either print the message “Nothing to upgrade” or run through some upgrades.
  2. Xdebug
    1. The PHPUnit installation guide states that “PHP_CodeCoverage, the library that is used by PHPUnit to collect and process code coverage information, depends on Xdebug 2.0.5 (or later) but Xdebug 2.1.3 (or later) is highly recommended. “. Here are the official Xdebug installation instructions. Unfortunately, I didn’t document this installation, but it did go exactly as the instructions explain. There weren’t any caveats.
      1. Install Xdebug:
        pecl install xdebug
      2. If the previous step fails because of this error: ERROR: `phpize' failed, and you are on Debian, like me, just run: apt-get install php5-dev and then repeat the previous step to install Xdebug (Reference).
      3. Add this line to your php.ini file:
        zend_extension="/usr/local/php/modules/xdebug.so"
    2. Make sure Xdebug was installed properly: If you have PHP for the command line installed, just run php -m. This will output a list of all PHP modules installed. If you see xdebug on the list after install, then it was successful.
  3. Install PHPUnit with the PEAR installer:
    1. pear config-set auto_discover 1
    2. pear install pear.phpunit.de/PHPUnit
  4. You should now be able to run phpunit from the command line.

How to Create a New Project with Zend Framework 1.11 on Linux

Prerequisites

  1. The following LAMP (Linux Apache MySQL PHP) stack should already be setup and installed:
    1. Linux installation (I use CentOS, but would recommend Ubuntu or Debian if you are new to this stuff)
    2. Web server (I use Apache)
    3. PHP
    4. Note: MySQL is not a requirement for creating a Zend Framework-based website.
  2. Basic understanding of how to manage a basic LAMP stack.
  3. Basic understanding of working at the command line on a Linux OS.
  4. You probably already know all of these things if you are even reading this…

Step 1: Download Zend Framework 1.11

  1. Navigate to http://framework.zend.com/download/latest in your browser.
  2. There are full and minimal versions available. I am going to be downloading the full version. Copy the tar.gz link for the full version.
  3. Log in to your Linux box and navigate to /usr/local/src
  4. Create a new directory named zend: mkdir zend
  5. Change the directory: cd zend
  6. Download the tar.gz file by using wget: wget http://framework.zend.com/releases/ZendFramework-1.11.11/ZendFramework-1.11.11.tar.gz
  7. Extract the contents of the tar.gz file: tar -xf ZendFramework-1.11.11.tar.gz
  8. You might want to delete the tar.gz file, but that is your choice.

Step 2: Locate the ZF Command Line Tool

  1. If you followed the directions in step 1 above, you should be able to find the extracted framework files at a location like this: /usr/local/src/zend/ZendFramework-1.11.11
  2. The ZF command line tool can be found in that directory, as bin/zf.sh. The absolute path to the zf.sh tool on my box is: /usr/local/src/zend/ZendFramework-1.11.11/bin/zf.sh
  3. Copy that absolute path, or remember where it is. We will need it in the next step.

Step 3: Create a new ZF Project (Website) with the ZF Command Line Tool

Note: I prefer to only use the ZF command line tool for creating new projects. Some people continue to use it as a development tool. I don’t. When I started developing with the Zend Framework, the command line tool only served to obscure how ZF functions. I suggest that you do the same – use the tool to create your new project and then forget about it. But, the choice is yours, and there is documentation out there on the interweb either way. Follow these steps to create your new project.

  1. Navigate to your web server’s public root directory. On my box the public root is located at /var/www/html
  2. Using the absolute path to the zf.sh tool, create your new project: /usr/local/src/zend/ZendFramework-1.11.11/bin/zf.sh create project zend_example
  3. You should now see the following file structure at /var/www/html/zend_example:
    /var/www/html/zend_example
      │
      ├── application/
      │    │
      │    ├─ Bootstrap.php
      │    ├─ configs/
      │    │    │
      │    │    └─ application.ini
      │    │
      │    ├─ controllers/
      │    │    │
      │    │    ├─ ErrorController.php
      │    │    └─ IndexController.php
      │    │
      │    ├─ models/
      │    └─ views/
      │         │
      │         ├─ helpers/
      │         └─ scripts/
      │              │
      │              ├─ error/
      │              │    │
      │              │    └─ error.phtml
      │              │
      │              └─ index/
      │                   │
      │                   └─ index.phtml
      ├─ docs/
      ├─ library/
      ├─ public/
      │    │
      │    ├─ .htaccess
      │    └─ index.php
      │
      ├── tests/
      └── .zfproject.xml

Step 4: Link the Zend Framework Library to your Project

Remember the location that you extracted the ZF files to? Create a symlink in your project to that location:

  1. Navigate to your projects ‘library’ directory: cd /var/www/html/zend_project/library
  2. Create a symlink to the Zend Framework library: ln -s /usr/local/src/zend/ZendFramework-1.11.11/library/Zend/ ./

Step 5: Test Your Project in a Web Browser

  1. At this point, if you navigate to your project (something like: http://127.0.0.1/zend_example/public/), you should see a similar “welcome” page as in this screenshot:
  2. If you want to change that default “welcome” page, you will find the actual “view partial” file that represents that screenshot at /var/www/html/zend_example/application/views/scripts/index/index.phtml. You can edit that file and refresh your browser to see your changes.

What Next?

  1. Learn about the following Zend components (“components” is just a general term I am using for Zend “things”):
    1. Controllers (/zend_example/application/controllers/IndexController.php).
    2. View “partials” (/zend_example/application/views/scripts/index.phtml)
    3. The Zend application Bootstrap file/class (/zend_example/application/Bootstrap.php)
    4. The Zend application configuration file (/zend_example/application/configs/application.ini)
  2. Read about the Model-View-Controller (MVC) design/architectural pattern. ZF is based on this pattern. The basic idea is that an application (or website) that is designed to follow MVC guidelines will neatly separate the “presentation” layer from the data. Data is your “model” and it encompasses everything from files to databases, to web services that return data. Keep in mind that the model is not limited to databases. A simple PHP file that contains an array or some variables is considered part of the model. Any data source, or code that accesses a data source is part of the model. If some code contains “data”, it is part of your model.
  3. Personally, I have chosen to ignore the Zend model classes (the M in MVC). The developers seem to have focused primarily on the “View” and “Controller” part of MVC, leaving the implementation of the “Model” up to the end-users (you and me). At work, we use existing non-Zend libraries for our model, as well as custom model classes. We symlink these libraries into each individual Zend project, just as we did with the Zend Framework library itself (in step 4 above).
  4. There is not a lot of great documentation available on the internet or in books about developing Zend Framework applications. I have found that the best documentation is the code itself (i.e. the actual class files in the Zend Framwork /library/Zend directory).

I welcome your comments, questions, and criticisms.

 References & Useful Links:

Here boy! Heredoc! Nowdoc!

Whenever I read “here document” or “heredoc” I immediately think about calling for a dog. Here’s an illustration by Frances Tipton Hunter from the December 5th, 1936 issue of The Saturday Evening Post (Reference), titled “Here Boy!”:

Heredoc

A here document (a.k.a. here doc, heredoc) is a way to represent a string exactly as it is written in your code, including line breaks and any whitespace (a.k.a. string literal). It is similar to the idea of preformatted text (e.g. the <pre></pre> element in HTML), with the added behavior of displaying the value of variables within the string (variable substitution). In other words, if you include a variable in your string, it will be parsed and the value of the variable will be displayed instead of the variable name. If you would usually use double-quotes to represent a string, but you want the string to occupy multiple lines in the actual code, then use a heredoc.

Heredoc Example:

//The PHP code:
$foo = 'bar';
$heredoc = &lt;&lt;

The heredoc code above will output:

Heredoc:
Hello
World
$foo = bar

I like to use heredocs when I have a long string of SQL, just for the fact that it makes the code easier to read:

$dbh = get_pdo_dbhandler($host, $dbname, $user, $pass);
 
//Define SQL query as a heredoc for pretty formatting/readable code
$sql = &lt;&lt;prepare($sql);
 
echo $sql;

The example above will output as follows:

SELECT id, fname, lname, email, created_date
FROM some_table
WHERE fname = 'Moore'
AND created_date &lt; 1334278718
ORDER BY created_date DESC
LIMIT 10
OFFSET 20;

EOD?

According to this wikipedia page, EOD means end of data. In the context of a heredoc or a nowdoc, EOD is a delimiter. It is used to define the beginning and end of the string. You can use any custom delimiter that you want, just as long as it is unique within your script.

Example not using EOD:

$example = &lt;&lt;

Nowdoc

A now document (a.k.a. now doc, nowdoc) is like a heredoc, except that it does not parse variables. If you have a string that you would usually represent with single quotes, but you want it to occupy multiple lines, use a nowdoc.

Nowdoc Example:

Note: EOD must be surrounded by single quotes in a heredoc.

$foo = 'bar';
$nowdoc = &lt;&lt;

The nowdoc code above will output as follows:

Nowdoc:
Hello
World
$foo is not parsed in a nowdoc.

Examples

Summary

  • Use a heredoc where you would usually use double-quotes.
  • Use a nowdoc where you would usually use single-quotes.
  • EOD means end of data

I recommend that you read the PHP manual page on strings. It includes descriptions and examples of the heredoc and nowdoc.

 

Linux Command Line Keyboard Shortcuts

I always forget these useful commands:

  1. Move cursor to beginning of line: [Command + a] (Mac), [Ctrl + a] (Linux/Windows)
  2. Move cursor to end of line: [Command + e] (Mac), [Ctrl + a] (Linux/Windows)
  3. Erase the current line: [Ctrl + u]
  4. Erase all text from the cursor to the end of the line: [Ctrl + k]
  5. Erase an entire word that occurs before the cursor: [Ctrl + w]
  6. Clear the terminal: [Ctrl + l]

I use 3, 5 and 6 a lot. There are many times where I wish I could remember the commands for 1 and 2. I think 4 could be useful.

References

  1. tuxfiles: Linux keyboard shortcuts – 2.0