Zend Framework Project in a sub-folder

We had a problem at work where we needed to deploy a Zend framework project to a subfolder on the server.  This is a new server for us, and the first time this situation has came up.  It took us a few hours to figure out the solution as we could get one thing working and then another would break.  So to try and prevent that for someone else I'm sharing what I learned.

The server we are using is on a subdomain and soon to have multiple Zend projects.  We couldn't use multiple sub-domains, which would have made this much easier.

So the problem at hand is that we need to deploy a Zend Framework project to a subfolder using a Virtualhost.  In this particular case there is a static index.html page that needs to be served up at the root of the subdomain (not everyone will have this situation, but it's what we needed).

The svn checkout in a nutshell:

/admin.myserver.com
    L webadmin
    |   L index.html
    L service
        L application
            L bootstrap.php
        L docs
        L library
        L public
            L index.php
            L .htaccess
        L tests

So let me set the stage.

  • http://admin.myserver.com/service is the url that we want the ZF project to be served out of.
  • The application has multiple virtual folders/controllers and parameters that need to be called. e.g.  http://admin.myserver.com/service/reps?name=Bob%20Smith
  • /admin.myserver.com/service/public is the SVN location of the ZF project to be served up to apache
  • /admin.myserver.com/webadmin is the location of index.html that is going to consume the service.

So what we are doing is creating a virtual host for admin.myserver.com. The key thing is creating the Alias for the /service, so when we go to http://admin.myserver.com/services the public folder of our app is used. Next configure the settings for the front end HTML directory that will be served at the root of the domain. We then configure the settings for the Zend application. Pretty straight forward, but if you aren't sure where to start it's hard to find.

Apache vHost for project:

<VirtualHost *:80>  
    DocumentRoot "/Source/admin.myserver.com/webadmin"
    ServerName admin.myserver.com
    Alias /service /Source/admin.myserver.com/service/public

    <Directory "/Source/admin.myserver.com/webadmin">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        order allow,deny
        Allow from All
    </Directory>

    <Directory "/Source/admin.myserver.com/service/public">
        Options Indexes FollowSymLinks MultiViews ExecCGI
        AllowOverride All
        order allow,deny
        Allow from All
    </Directory>
</VirtualHost>  

Next we edit the Bootstrap.php in the Zend project.  Some examples I came across had this set in the application.ini, but I couldn't get that to work, so this is what we ended up with.  The key thing is that we are setting the setBaseUrl to the subfolder where the application lives.

protected function _initSetupBaseUrl() {  
    $this->bootstrap('frontcontroller');
    $controller = Zend_Controller_Front::getInstance();
    $controller->setBaseUrl('/service'); //the subfolder your app is in
}

This is where the tricky part is.  Because I'm on a Mac I didn't see the .htaccess file in the Finder.  The .htaccess resides in the /service/public folder.  The contents of this file could be migrated to the Directory settings in the services/public location and delete the .htaccess file if you so desire.  The addition of RewriteBase is the key to this file.  This is the piece that took us (read: me) forever to find.

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

So I hope this helps out someone else so they don't waste a ton of time like I did searching for the answers.

Ray Pierce

Web Developer, RetailMeNot, Mentor, NodeBot builder, Texan

Austin, TX Ya'll http://digitalpierce.com
comments powered by Disqus