Use Zend Framework in subfolder

What do you need to do? Nothing! Put it in a subfolder.

Trying to find the awnser on the question: “How do you use Zend Framework in a subfolder” a lot of resources I found referred to an article on Alexander Netkachev’s blog. At the time of writing Alex is reviewing all of his articles, so I had to figure this out one myself. Hopefully Alex’s article will be online anytime soon. I’m sure it contains valuable information. However, I guess it is not the information I was looking for. On Zend Devzone Carl Evans refers to Alex’s article and the 2 solutions found by Alex. He quotes the opening paragraph, revieling some information about 2 provided solutions in the last few lines:

First is rather a hack then a recommended solution, but with it you can start the applications and examples that use standard router. The second is neater but it depends on the Zend_Controller_RewriteRouter class.

A hack? Depends on the router? Not sure we are adressing the same problem here. Maybe his solution is for people having no access to the directory above the public directory. I don’t know. My solution is almost to easy. It only needs minor editing of index.php. Off course there is are some caveats.

Firstly I will make clear what we are talking about here. What is the folder structure we are creating? Next I will show you how to put your Zend Framework in a subfolder. Be sure to also read the conditions to be met when using this solution. As you might expect, these are mainly about links to pages and resources inside your own domain. The Zend View Helpers BaseUrl and Url are key to our solution.

Sub folder structure for your Zend Framwork project:

To be clear, this is the directory structure I’m looking for:

application/
library/
project1/
    application/
    library/
secondproject/
    application/
    library/
otherproject/
    application/
    library
public/
    css/
    imgages/
    js/
    .htaccess
    index.php
    project1/
        css/
        imgages/
        js/
        .htaccess
        index.php
    secondproject/
        css/
        imgages/
        js/
        .htaccess
        index.php
    otherproject/
        css/
        imgages/
        js/
        .htaccess
        index.php

Each project’s application, library, etc. (see Zend Framework recommended file structure) in it’s own project folder. One directory up from the public directory (as recommended).

Inside the public directory each project has it’s own folder containing index.php & .htaccess files and css , js etc. folders.

The solution

Put your files like the structure above. For each project, open the index.php file and adjust the APPLICATION_PATH and php_include_path. For project1:

'/../application'

now becomes

'/../../project1/application/'

Same goes for php_include_pahts. All projects can live on one set of Zend_ files. Change paths accordingly. Many tutorials will tell you to set the baseUrl. You don’t set it (you can off course), you use it. Done, simple enough?

The first cotcha I ran into is my .htaccess file in the main public folder was setting the APPLICATION_ENV. Be aware of this.

Use of the Zend View Helpers BaseUrl and Url

The second thing to check are urls for navigation to other pages on the website. They all have to start with ‘/project1′ (if you look at pages inside project1 folder). This means that if you want to use your projects in sub-folders, you have to use either the Zend_View_Helper_BaseUrl or the Zend_View_Helper_Url to create your internal referring urls. See documentation for proper use of there helpers. One example:

before you would have:

/users/peter/id/3

you will now either use:

$this->baseUrl('users/peter/id/3')

or:

$this->url(array('id'=>'3'),'default')

Zend Navigation

When using Zend_Navigation for creating menu’s etc, you have to define all modules, controllers, actions, routes etc. Yes, I know.. This solution is only for people going ‘all the way’ with the Zend_Navigation. You can’t use Zend_Navigation with uri based navigation descriptions. You don’t have to use Zend_Navigation, the options stated above work as well.

Css files, image paths relative

Don’t forget to use the baseUrl helper on you css file paths, same applies for javaScript files.

$this->headLink()->appendStylesheet($this->view->baseUrl('css/style.css'));

What about the background images used in css? Write your image urls relative (without starting ‘/’  ) to the path of the css file referring to them. This means location of css file and  its images are bound to each other, relatively. Put css in your css folder, background images in a css/img folder. You’ll be fine.

Summary how to put Zend Framework in a subfolder

  • Give project’s resources (application, library) a folder in the sub directory  from public directory
  • Create a subfolder for public files, index.php, .htaccess, css-files etc.
  • Change APPLICATION_PATH in index.php
  • Change php_include_paths in index.php

Check the following:

  • Zend_Navigation must use MVC pages
  • Urls are created with
    • View helper: url
    • View helper: baseUrl
  • Don’t forget css and javaScript files
  • background-images have relative path

Did this help? Are you stuck? Do you need an other folder structure? What other things did you run in to applying this method? Let me know!


Leave a Reply