Nov 18 2008

Zend Framework Quickstart Tutorial: Deploy to a subdirectory instead of web root

Published by John at 12:45 pm under geek

When you’re just evaluating something new you do not want to rearrange your whole world just to try something out.

I wanted to try out the Zend Framework and the “QuickStart” tutorial looked very official and the ideal place to start.

However they assume installation is in the webroot. Sorry, but I have other, more important, stuff running on this server and I’m not shoving it aside just for a quick tutorial.

Try as I might, I had great difficulty getting this tutorial to run in a subdirectory. As I’m learning the thing I’ve no idea how to tweak it yet!

Anyway my web root is “/var/www” and I want to put the quickstart tutorial at “/var/www/QuickStart”, which in turn means the public directory is at “/var/www/QuickStart/public”. To make the whole thing work required three changes:

1. Redirect everything to the index in the subdirectory

When I use the cited .htaccess from the tutorial page titled “Create a Rewrite Rule” and call the index it works! Great, except that’s not the test for ‘working’.

Zend needs to feed all requests through the index page so it can farm them out to controllers. The controllers are identified by the ‘filename’ of the request: so the redirection is supposed to catch all the 404’s and feed them into the index. It’s not working until you can call non-existent URL’s without the webserver giving you a 404. eg: “http://servername/QuickStart/public/asdf” should show the “Hello, Zend Framework!” message.

Stay on this step until you get the “Hello” message for made up url under your public directory. E.g. “http://servername/QuickStart/public/asdf”.

My /QuickStart/public/.htaccess became (NB: this is Case Sensitive)


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

This might be done more cleanly with a “RewriteBase” but this worked so I moved on.

2. Set the “BaseURL” of the front controller

Now there is something not right in the Tutorial because the next checkpoint screenshots a message which doesn’t appear in the code so far displayed. That in itself is very frustrating and entire problem of its own so lets not get distracted. I tried to run through the tutorial again from scratch so I could make this post straight from a fresh run through. But not to be.

If you manage to overcome and reach the next checkpoint you’ll need this:

Add a line to “Step 3″ of the the /application/bootstrap.php to set the base url:


// Step 3: CONTROLLER DIRECTORY SETUP - Point the front controller to your action
// controller directory.
$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
$frontController->setBaseUrl('/QuickStart/public');

This tells the front controller to prepend Url’s with the subdirectory, and this got me most of the way through the QuickStart tutorial with one exception.

3. Fix the “Action” in guestbook form

When you post to the guestbook you’ll get an error looking for a file “/guestbook/sign” which doesn’t exist. The form is created with that action and you need to fix it in the GuestbookController.php:

Change the setAction in function _getGuestbookForm() to


$form->setAction($this->_request->getBaseUrl() . $this->_helper->url('sign'));

And with that it should generate a form action “/QuickStart/public/guestbook/sign”.

I’m told this last issue has been lodged has a defect and not necessary from releases “1.7″ and beyond. The helper->url will henceforth prepend the baseUrl to its result.

Trackback URI | Comments RSS

Leave a Reply