2012-08-08 68 views
0

我有上zend_router_hostname“动态子域”的问题,我有这样的代码(我没有这样的,但whant使用子域名作为路线parametr子域):如何在Zend Framework中路由动态子域?

protected function _initRoutes() { 
     $front = Zend_Controller_Front::getInstance(); 
     $router = $front->getRouter(); 
     $config = $this->getOptions(); 
     Zend_Registry::set("config", $config); 
     $routerHost = new Zend_Controller_Router_Hostname(':language.mysite.local', 
         array('controller' => 'index', 
          'action' => 'index', 
          'language'=>'pl') 
); 
     $router->addDefaultRoutes(); 
     $routes = $router->getRoutes(); 
     foreach ($routes as $key => $routeEntry) { 
      $router->addRoute($key, $routeHost->chain($routeEntry)); 
     } 
    } 

但是当我尝试做电如。 :pl.mysite.local我得到服务器未找到错误。 我的/ etc/hosts文件是:

127.0.0.1  localhost 
127.0.2.1  mysite.local 
127.0.3.1  mysite.dev 
127.0.4.1  mysite.production 

我的虚拟主机配置是:

VirtualHost *:80> 
    DocumentRoot "/var/www/mysite/public" 
    ServerName mysite.local 
    ServerAlias *.mysite.local 
    # This should be omitted in the production environment 
    SetEnv APPLICATION_ENV development 
    <Directory "/var/www/mysite/public"> 
     Options Indexes MultiViews FollowSymLinks 
     AllowOverride All 
     Order allow,deny 
     Allow from all 
    </Directory> 

</VirtualHost> 

我试图用的dnsmasq与像条目:

address=/local/127.0.0.1 

但问题仍然存在,我不知道如何解决这种情况,任何线索我做错了什么?

+0

不要让你的应用程序复杂。首先在服务器配置中路由子域,然后在Zend Framework中执行标准路由。你的应用程序会说声谢谢你。 – hakre 2012-08-09 12:12:48

回答

2

将通配符添加到/ etc/hosts是不可能的,但在我们的例子中也是必需的。 然而,这是怎么去做:

安装dnsmasq

然后,

cp /usr/share/doc/dnsmasq-base/examples/dnsmasq.conf.example /usr/local/etc/dnsmasq.conf 

然后,编辑您/usr/local/etc/dnsmasq.conf并添加:

address=/mysite.local/127.0.0.1 
listen-address=127.0.0.1 

开始DNSMASQ:

~$ sudo ./usr/local/sbin/dnsmasq 

而且,而是采用Zend_Controller_Router_Hostname使用Zend_Controller_Router_Route_Hostname

经过文件,你应该OK!

$toRoute = new Zend_Controller_Router_Route_Hostname(
    ':language.mysite.local', 
    array(
     'controller' => 'index', 
     'action' => 'index', 
     'language' => 'pl') 
    )); 

PS:别忘了在/etc/resolv.conf中提到nameserver为127.0.0.1。有时,重新启动系统有帮助!

享受编码! :)

+0

已更改为在/ etc/hosts中,仍然没有结果... – Axxxon 2012-08-09 07:34:23

+0

我明白了,问题是无论您在路由器中定义的是否作为后缀检查到您的基本域。例如::xyz/yes将会是example.com/xyz/{value}/yes,既然你已经使用了dot,它也会给你一个错误!无论如何,让我检查。但是,现在主机文件已正确设置,请等待路由器脚本。 – Karma 2012-08-09 08:07:16

+0

@Axxxon我已编辑帖子。检查使用的正确功能(提供链接)。另外,选中它是正确的,如果它有效,如果它没有,那么让我知道发生了什么! – Karma 2012-08-09 08:12:22