使用Zend_Controller_Router_Route_Hostname在多个域上运行相同的Zend_Framework站点

时间:2010-11-29 15:47:04

标签: php zend-framework zend-controller-router

我需要运行基于zend_framework的同一站点才能在多个域上运行,我的应用程序需要知道它运行在哪个域上。 我认为使用Zend_Controller_Router_Route_Hostname是一个好主意,但我遇到了一些问题。

我的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));

我收到以下错误“未指定子域”。 我想我在我的布局文件中跟踪到$ this-> url是在路由与请求网址匹配之前使用已定义的路由来组合网址。 如果我将默认值设置为主机名路由,则不会收到错误,但布局中的URL使用默认值而不是当前主机名中的值。

谁能帮我解决这个问题?

2 个答案:

答案 0 :(得分:1)

您应该为子域定义默认值。将$ defaultRoute更改为:

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

它应该有用。

答案 1 :(得分:1)

您在网址生成方面遇到了问题。您应该在主机名的路由定义中为变量指定参数,如下所示:

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

否则,Zend URL帮助程序无法为您的定义生成URL :sub-domain。:domain。:tld。提供的示例将生成此url: your-subdomain-here.your-domain.com/current_controller/current_action

同时检查子域是否为合法变量名。尝试将其更改为子域。

相关问题