开发者

Run the same Zend_Framework site on multiple domains with Zend_Controller_Router_Route_Hostname

I need to run the same site based upon the zend_framework to run on multiple domains and my application needs to be aware on which domain it is running on. I thoug开发者_运维问答ht it was a good idea to use Zend_Controller_Router_Route_Hostname, but I have run into some problems.

I have the following code in my bootstrap

$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
$router->removeDefaultRoutes();     

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
    ':sub-domain.:domain.:tld'
);

$defaultRoute = new Zend_Controller_Router_Route(
    ':controller/:action/*',
    array(
        'controller' => 'index',
        'action' => 'index'
    )
);
$router->addRoute('default',$hostnameRoute->chain($defaultRoute));

I get the following error "sub-domain is not specified." I think I tracked it down to that $this->url in my layout file is using the defined routes to assemble the url before the route is matched against the request url. If I set default values to the hostname route I don't get the error but the urls in the layout use the default values instead of the values in the current hostname.

Who can help me solve this problem?


You should define a default for subdomain. Change $defaultRoute to this:

$defaultRoute = new Zend_Controller_Router_Route(
    ':controller/:action/*',
    array(
        'controller' => 'index',
        'action' => 'index'
    )
);

It should work.


You've got a problem in URL generation. You should specify parameters for variables in route definition for your hostname like this:

echo $this->url(array('sub-domain' => 'your-subdomain-here', 'domain' => 'your-domain', 'tld' => 'com'), 'default');

Otherwise Zend URL helper can't generate url for your definition :sub-domain.:domain.:tld . Provided example will generate this url: your-subdomain-here.your-domain.com/current_controller/current_action

Also check if sub-domain is legal variable name. Try to change it to subdomain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜