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

时间:2021-01-09 11:09:32

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 thought it was a good idea to use Zend_Controller_Router_Route_Hostname, but I have run into some problems.

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

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.

我得到以下错误“子域未指定”。我想我找到了那个$this->url在我的布局文件中使用已定义的路由在路径与请求url匹配之前组装url。如果我将默认值设置为主机名路由,我不会得到错误,但是布局中的url使用默认值,而不是当前主机名中的值。

Who can help me solve this problem?

谁能帮我解决这个问题?

2 个解决方案

#1


1  

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

您应该为子域定义一个默认值。改变美元defaultRoute:

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

It should work.

它应该工作。

#2


1  

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

URL生成有问题。您应该在路由定义中为您的主机名指定参数,如下所示:

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

否则Zend URL helper就不能为您的定义生成URL:子域。:域。:tld。提供的示例将生成这个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.

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

#1


1  

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

您应该为子域定义一个默认值。改变美元defaultRoute:

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

It should work.

它应该工作。

#2


1  

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

URL生成有问题。您应该在路由定义中为您的主机名指定参数,如下所示:

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

否则Zend URL helper就不能为您的定义生成URL:子域。:域。:tld。提供的示例将生成这个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.

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