douyan9417 2012-08-08 21:15
浏览 24
已采纳

如何在Zend Framework中路由动态子域?

I have problem with 'dynamic subdomains' on zend_router_hostname, i have code like(i don't have subdomains like that but whant use subdomain as route parametr):

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));
        }
    }

But when I try do call eg.: pl.mysite.local i get Server not found error. My /etc/hosts file is:

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

And my vhost config is:

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>

I tried to use dnsmasq with entry like:

address=/local/127.0.0.1

But problem still occurs, i dont know how to resolve this situation, any clue what i'm doing wrong?

  • 写回答

1条回答 默认 最新

  • doushi9376 2012-08-09 06:01
    关注

    Adding wildcards to /etc/hosts is not possible but, also required in our case. However, this is how to go about it:

    Install dnsmasq

    then,

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

    then, edit your /usr/local/etc/dnsmasq.conf and add:

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

    Start DNSMASQ:

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

    Further, instead of using Zend_Controller_Router_Hostname use Zend_Controller_Router_Route_Hostname

    Go through the document and you should be OK!

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

    PS: Don't forget to mention nameserver as 127.0.0.1 in /etc/resolv.conf . At times, restarting system helps!

    Enjoy coding! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决