Zend Framework 2 and Subfolders

zend-framework

Zend Framework 2 is a big step forward compared to the previous version. Rob Allen also created the Zend Skeleton application you could use as a start point for your own development.

The Zend Skeleton application was created with the best practice in mind, i.e. no code in the root, so here are two common problems:

1) How to access an ZF2 application without “public” in URL

In order to access the application you either need to include “/public” part in URL or to setup virtual host. But what if you find yourself in a situation that your client wants to have the ZF2 application as a subfolder of their original website or they have shared hosting account without ability to setup virtual host? In that case you are forced to include public at the end of the URL, e.g. www.site.com/zf2/public

2) How to access static files

Another scenario is to have additional non-ZF2 script as a subfolder of your ZF2 application. The Zend Skeleton application doesn’t allow you to do anything similar because all requests are handled by index.php.

Solution

Maybe you could solve problems by changing .htaccess files, but the only solution I found to efectively solve both problems is to transfer all files and folders from the public folder to the root folder of the application.

Here are instructions:

1) Copy files and folders from application’s public folder to its parent directory (i.e. the application’s root). The following should be copyied:

css
images
js
.htaccess
index.php

Be sure that you’ve replaced .htaccess in the root from .htaccess file from the public directory

2) Comment out the following line in index.php

//chdir(dirname(__DIR__));

Now you are free to organize your or client’s website as you wish, you can both add ZF2 application into website’s subdirectory and install any other 3rd party script into the ZF2 application as well.

If you have any other suggestion(s) add in the comment form below.

Share This Post

Related Articles

2 Responses to “Zend Framework 2 and Subfolders”

  1. Gaston says:

    Hi Thanks for posting this one question.

    Should I change something on the .htaccess? if so what should I add or change?

    Thanks

    • LoneShooter says:

      Hi, Gaston. No, you don’t need to change anything in the .htaccess file, but it’s important to copy .htaccess file from the public folder in your application’s root folder, as I wrote in the article.

Leave a Reply

© 2024 LoneShooter.com. All rights reserved. Site Admin · Entries RSS · Comments RSS