【问题标题】:Zend Framework: get subdomain parameter from routeZend Framework:从路由获取子域参数
【发布时间】:2011-07-08 01:35:46
【问题描述】:

更新: 解决了。问题是因为我们使用 nginx 作为前端。所以 nginx 不会将 HTTP_HOST 传递给 apache。


你好!

我在生产服务器上的基本控制器中获取子域参数时遇到问题,而在本地主机上没问题。来自 url 的其他参数,如控制器,按应有的方式返回操作。

这在生产中返回 null:

$agencyName = (string) $this->_getParam('agency');

没有对 .htaccess 进行任何更改:

RewriteEngine On
RewriteRule ^main - [L,NC]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

这是我的虚拟主机设置:

<VirtualHost *:8080>
        ServerName  agencies.domain.com
        ServerAlias *.agencies.domain.com

        ErrorLog /var/log/apache2/agencies.domain_errors.log

        DocumentRoot /var/www/agencies.domain.com/public/

        <Directory "/var/www/agencies.domain.com/public">
                Options -Indexes FollowSymLinks Includes
                DirectoryIndex index.shtml index.php
                AllowOverride All
                # Controls who can get stuff from this server.
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

有人知道为什么会这样吗?

更新:

Bootstrap 中的路由器

public function run()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

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

        $config = $this->getOptions();

        $hostnameRoute = new Zend_Controller_Router_Route_Hostname(
                        ':agency.' . $config['siteUri'],
                        NULL,
                        array(
                            'agency' => '([a-z0-9]+)'
                        )
        );

        $router->addRoute('subdomain', $hostnameRoute->chain($plainPathRoute));

        parent::run();
    }

是的,我确实定义了 $config['siteUri'] 并且我也尝试使用 :agency.domain.com 再次遇到同样的问题

【问题讨论】:

  • 看来我们需要检查您正在使用的路线。
  • 请创建一个答案,而不是回答答案中的问题。稍微阐述一下。

标签: apache zend-framework mod-rewrite nginx


【解决方案1】:

使用以下内容:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRoute()
    {
        $this->bootstrap('FrontController');
        $router = $this->getResource('FrontController')->getRouter();
        $router->removeDefaultRoutes();
        $plainPathRoute = new Zend_Controller_Router_Route(
                        ':module/:controller/:action/*',
                        array(
                            'module' => 'default',
                            'controller' => 'index',
                            'action' => 'index',
                        )
        );
        $router->addRoute('default', $plainPathRoute);
        $config = $this->getOptions();
        $hostnameRoute = new Zend_Controller_Router_Route_Hostname(
                        ':agency.' . $config['siteUri'],
                        NULL,
                        array(
                            'agency' => '([a-z0-9]+)'
                        )
        );
        $router->addRoute('subdomain', $hostnameRoute->chain($plainPathRoute));
    }
}

如果您提供了有效的子域(即仅由字符 a-z0-9 组成),它将在代理中传递,否则将不会设置代理。 (至少它适用于我使用 ZF 1.11.3 :p)。

【讨论】:

  • 我在使用它时遇到了同样的问题。我的路由器在本地主机上工作正常。这意味着问题不在于他们,而在于 apache 方面的某个地方。
  • 你真的在你的本地主机上用子域测试过它们吗?如果它在本地工作并且不能在线工作,那么它可能是应用程序中的配置问题(即您的 siteUri 可能是错误的)或 apache 配置中的问题。顺便说一句,您更改设置后是否重新启动了 apache?
【解决方案2】:

解决了。问题是因为我们使用 nginx 作为前端。所以 nginx 不会将 HTTP_HOST 传递给 apache。

【讨论】:

    猜你喜欢
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多