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.
Good job John,
I started playing with ZF 1.7 this am – and I also found the QuickStart quite frustrating – when I saw that the error handler wasn’t working I realized it was a sub-directory problem, googled – your tips were right on
they’ve fixed that last issue in 1.7 fwiw.
Also had to chmod a+rw the sqlite database in the demo to get it to work
10x
Danny
Great, this was very helpful. Yes, everything works with point 1 and 2 alone. Point 3 should be skipped …I tried for ZendFramework-1.7.4
thank you! i spend so much time on this problem. works great.
Thank you John
I was trying to create a sample project on ZF by referring the quick start and was about to give up due to the errors. Your article helped a lot and the project is done.
Holy crap you have no idea how helpful this post was!
I spent a good 5 or 6 hours trying to get my Zend app running on a shared host, and everything I did kept trying to use “{appname}/public/index.php/” as the base url.
Changing the final line of the .htaccess file from “/index.php” to “{appname}/public/index.php” made everything work, thanks!
I have been trying unsuccessfully to deploy the framework above the web root but run index.php from a subdirectory:
eg. web root = “/var/www/” and “/var/www/subdir/index.php”.
The controllers just don’t run and I get 404s
My .htaccess looks like to original and is in the subdirectory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ – [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Jojo:
1. The browser will call for the subdirectory:
eg: http://example.com/subdir/
2. Take another look at the last line of your .htaccess file. Are you redirecting everything to the index.php in the subdirectory? or sending everything back to the index.php in the root directory?
Thanks John, it seems I should have had my .htaccess in the web root and not in the /subdir and obviously be redirecting with:
RewriteRule ^.*$ /subdir/index.php [NC,L]
Now all of my existing (non framework) content can remain in web root and I can now run Zend framework via the subdir whilst keeping all the framework file above web root. So it is true, the framework is flexible to individual needs.
Many thanks
Been trying to setup in a sub-dir but not by putting the entire project in the sub-directory, just the contents of the public folder i.e. moving the public folder out of the application and into a sub-directory of the web root so that the application code itself is not web accessible. Was able to get the default index page working by modifying the application path to point to the application location, but any other controller would not work cause it was assuming the subdir was the controller. The rewrite rule (last line) was all I needed to change,to get it to work.
Thanks!!!
For ZF 1.8, it doesn’t appear as if this is necessary at all. I was having trouble getting rewriting to work right & thought this was the solution, but it was my AllowOverride in apache2.conf in Ubuntu. I just reverted to the .htaccess created by the zf create project command, and it seems to work fine.
Thank you John,
Like others – I had the same problem in Zend QuickStart tutorial. Last .htaccess line in Your example didn’t help, but it gave me idea to write it:
RewriteRule ^.*$ /ZendTest/index.php [NC,L]
And it worked, now I’m back on my way.
Many thanks!
P.S. I’d like Zend tutorial would have the same “Comments on this page” idea, as PHP Manual have
Hi John,
Why not use a vhost as recommended in the documentation?
Simon
HI John,
Many thanx to you.
Your htaccess configuration worked for me!