Zend Framework in subdirs

It took me some time to find this out so maybe it’s useful for you aswell…
I went through the very good tutorial written by Chris Shiflett concerning Zend Framework and was confontend with a little problem. Everything worked fine except the redirection by the Controller objects, I always was redirected to the noRouteAction. The problem is that I installed the application in a vhost subdirectory (http://devserver/subdir/) and it kept redirecting me to the server root. To “fix” this issue one have to make use of the Zend_Controller_RewriteRouter class. The following code snippet shows the usage:

< ?php
include 'Zend.php';

Zend::loadClass('Zend_Controller_Front');
Zend::loadClass('Zend_Controller_RewriteRouter');

$controller = Zend_Controller_Front::getInstance();
$controller->setControllerDirectory(
'/path/to/controller_classes');

Zend::register('controller', $controller);
$router = new Zend_Controller_RewriteRouter();
$controller->setRouter($router);
$controller->dispatch();

?>

2 Responses to “Zend Framework in subdirs”

  1. Cred Says:

    Thank you so much for this! I tried http://www.alexatnet.com/Blog/Index/2006-09-26/using-zend_controller-in-subfolder but that didn’t work, your piece of code works fast and fine :)

  2. Thapole Says:

    Np :)

    Unfortunately I experience some problems with the new preview snapshot. Does this code still work for you with 0.2.0?

Leave a Reply