Zend Framework: get subdomain parameter from route

2019-06-25 04:42发布

UPD: Solved. The problem was because we're using nginx as a frontend. So nginx doesn't pass the HTTP_HOST to apache.


Hi there!

I'm having a problem with getting subdomain parameter in my base controller on a production server while on the localhost it's ok. other parameters from url like controller, action returned as they should.

this returns null on production:

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

no changes made to .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]

and here's my vhost settings:

<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>

Does anybody knows why it happenes?

upd:

routers in 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();
    }

and yes, I do have $config['siteUri'] defined and i also tried using :agency.domain.com getting the same problem again

2条回答
Summer. ? 凉城
2楼-- · 2019-06-25 05:30

Use the following :

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

If you provide a valid subdomain (ie. only consisting of characters a-z0-9), it will be passed in agency, if not then agency will not be set. (At least it works for me using ZF 1.11.3 :p).

查看更多
神经病院院长
3楼-- · 2019-06-25 05:36

Solved. The problem was because we're using nginx as a frontend. So nginx doesn't pass the HTTP_HOST to apache.

查看更多
登录 后发表回答