Category Archives: Zend Framework

Example virtual host and .htaccess configuration for Zend application on OSX Lion

I usually work with the backend of web applications so I do not get enough training in setting up virtual hosts, so I thought I better put an example up here. At least to have something to start with.

I use the built in apache server on OSX and I usually put my virtual hosts in the <username>.conf (/private/etc/apache2/users/<username>.conf) where <username> is simply your username 🙂


<VirtualHost *:80>
        DocumentRoot "/Users/<username>/Sites/myzendapp"

        <Directory "/Users/<username>/Sites/myzendapp">
                Options Indexes FollowSymLinks
                AllowOverride FileInfo
                Order allow,deny
                Allow from all


To get to work we need to put an entry in the hosts file like this:

# Host Database
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##       localhost broadcasthost
::1             localhost 
fe80::1%lo0     localhost

To reach your new zendapp go to:

If you want to use instead just add the public part to the DocumentRoot:

DocumentRoot "/Users/<username>/Sites/myzendapp/public"

and the Directory:

<Directory "/Users/<username>/Sites/myzendapp/public">
                Options Indexes FollowSymLinks

NOTE! Depending on your choice in urls you may need to change the .htaccess (/Users/<username>/Sites/myzendapp/public/.htaccess) file accordingly:

For long url (

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /public/index.php [NC,L]

For short url (

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]

Tested on OSX 10.7.4 and Zend Framework 1.10.8

Zend_Db syntax cheat sheet

A small cheat sheet with simple examples for the Zend_Db syntax in Zend Framework. A smooth way to write sql that will work on many different databases

Connect to database

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'     => '',
    'username' => 'myUser',
    'password' => 'myPassword',
    'dbname'   => 'myDatabase'

SELECT statement

SELECT fname, course, grade FROM students 
   WHERE student_id = {$student_id} ORDER BY grade DESC

is written in Zend_Db syntax like this:

  ->from('students', array('fname', 'course', 'grade'))
  ->where('student_id = ?', $student_id)
  ->order('grade DESC');

If you want to fetch all columns (*), just remove the array(‘name’,…) in the from function

INSERT statement

INSERT INTO students(id, email, passwrd, fname, address, active) 
   VALUES (id + 1, '{$email}', '{$passwrd}', '{$fname}', '{$address}', 1);

Zend_Db syntax:

$student = array( 'id' => new Zend_Db_Expr('id + 1'),
                  'email' => $email,
		          'passwrd' => $passwrd,
		          'fname' => $fname,
		          'address' => $address,
		          'active' => '1');

$db->insert('students', $student);

UPDATE statement

UPDATE students
   SET passwrd = '{$passwrd}',
       fname = '{$fname}',
       address = '{$address}',
       active = 1
WHERE id = '{$student_id}'

Zend_Db syntax:

$student = array('passwrd' => $passwrd,
                 'fname' => $fname,
                 'address' => $address,
                 'active' => '1');

$db->update('students', $student, 'id = ' . $student_id);

DELETE statement

DELETE FROM students WHERE id = '{$student_id}'

Zend_Db syntax:

$db->delete('students', 'id = ' . $student_id);

Tested in Zend Framework 1.10.8 on OSX 10.7.4

Setup SQLite in Zend Framework

Like many things in Zend Framework this is very simple. I will here show how I usually do.

First we set the required parameters in application.ini:

;Setup database (SQLITE)
resources.db.adapter = "PDO_SQLITE"
resources.db.params.dbname = APPLICATION_PATH "/../db/basic.db"

This tells Zend that we will be using a SQLite database (PDO_SQLITE) and then also the path to the SQLite db file (basic.db)

For easy access we will now add the database handle to the Zend registry:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initDbAdapter() {
        $db = $this->getResource('db');
        Zend_Registry::set('db' , $db);

Then whenever we need to use the database we collect if from the Registry

// Get SQLite db handle from the registry
$db = Zend_Registry::get('db');

// Use the database
$resultset = $db->fetchAll("SELECT id, name FROM projects");


Tested on Mac OSX 10.6.8 and Zend Framework 1.10.8