Article explains how to use an application that is powered by Zend_Controller from Zend Framework in the subfolder of your web root.
This question I had heard many times and now I have a few minutes to write down the answer so that everybody who stuck in the similar trouble could quickly resolve the issue. Who works with Zend Framework, most likely, have read the “Zend_Controller / Getting Started†Zend Framework manual article, which explains how to use Zend_Controller in the root folder of the web server. But it does not answer on the “How to run my Zend Framework application in a subfolder of the web server document folder?†question and I propose you my solutions. 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.
The first solution is based upon that fact, that default router use the first element of the REQUEST_URI server variable as a controller name and the second one as an action name. So this variable can be overwritten with the modified value. Like this:
<?php
set_include_path('.;./ZendFramework');
require_once 'Zend/Controller/Front.php';
$_SERVER['REAL_REQUEST_URI'] = $_SERVER['REQUEST_URI'];
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], strlen('/subfolder'));
Zend_Controller_Front::run('./');
?>Please notice the '/subfolder' string constant—it is a Web path to the subfolder with the application, starting from the document root of the Web server. As I mentioned, the solution looks like a hack but it works.
The more elegant solution requires using Zend_Controller_RewriteRouter and its features. Consider the following code fragment:
<?php
set_include_path('.;./ZendFramework');
require_once 'Zend/Controller/Front.php';
require_once 'Zend/Controller/RewriteRouter.php';
$router = new Zend_Controller_RewriteRouter();
$router->setRewriteBase('/subfolder');
Zend_Controller_Front::getInstance()->setRouter($router);
Zend_Controller_Front::run('./');
?>The highlighted line with $router->setRewriteBase(...); call specifies where the application folder resides and router is taking this into consideration when it calculates the controller/action pair.
With a little bit of the PHP magic we can make the script more intelligent. Replace the highlighted line with the following code to perform automatic calculation of the path:
$router->setRewriteBase(str_replace('\\', '/',
substr(dirname(__FILE__), strlen(realpath($_SERVER["DOCUMENT_ROOT"])))));