I am building a Zend Framework site with a conventional directory structure i.e.
www.example.com/controller/action
I would like to insert the country code into the url i.e
www.example.com/us/controller/action
I would like to modify the baseUrl to /us so that the Zend Router will strip this off the url and will correctly assign the controller and action. If I do not modify the baseUrl setting, the Zend Router will think that the controller is 'us' and the action is 'controller'.
To achieve this, I tried adding a new function to my bootstrap.php as follows:
protected function _initBaseUrl() {
$region = "us";
$front_controller = Zend_Controller_Front::getInstance();
$request = new Zend_Controller_Request_Http();
$request->setBaseUrl($request->getBaseUrl() . '/' . $region);
$front_controller->dispatch($request);
}
This all works perfectly with standard urls and the Zend Router is able to strip of the country code before determining the route as required.
Unfortunately, it all goes wrong when I add custom routes into the picture. If I create a custom route in the bootstrap.php as follows:
public function _initRoutes() {
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'/local/:place/:index',
array(
'controller' => 'Local',
'action' => 'directory'
)
);
$router->addRoute('place', $route);
}
I should be able to enter a url like:
www.example.com/us/local/dallas/01
and Zend Router should take me to the directory action in the Local controller along with the parameters place = 'dallas' and index = '01'
This, however, throws an error. Removing the /us part of the url throws the same error i.e.
Fatal error: Call to a member function hasResource() on a non-object in C:\vhosts\dev.startyobusiness.com\application\controllers\ErrorController.php on line 49
The ErrorController.php file looks like this:
public function getLog()
{
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
where line 49 is the first line of the if statement. I tested the preceding line and
$bootstrap = $this->getInvokeArg('bootstrap');
returns null for $bootstrap.
It looks to me like I am dispatching the front controller before the custom route is set. Does anyone know the proper way to do this?