简体   繁体   中英

How to create user based virtual subdomains in a module based application in Zend Framework 1.12?

I am creating an application on zend framework 1.12 I have a user module which accepts a user login and registration from "www.example.com". After validating the user I want to redirect the user to user specific subdomain "username.example.com". I already have configured my hosts file and apache's vhost file.

Any help on this is appreciated.

Thanks in advance.

Here is the way which i have used in my project for multiple subdomains;

In index.php file extract the subdomain name (if exists any) and set it into a constant variable. This will be used later.

function extract_domain($domain) {
    if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches)) {
        return $matches['domain'];
    } else {
        return $domain;
    }
}
function extract_subdomains($domain) {
    $subdomains = $domain;
    $domain = extract_domain($subdomains);
    $subdomains = str_replace('www', '', rtrim(strstr($subdomains, $domain, true), '.'));
    return $subdomains;
}

$subDomainName = extract_subdomains($_SERVER['HTTP_HOST']);
//this const will be used in bootstrap file.
define('SUBDOMAIN_NAME', $subDomainName);

After login save the name of the sub domain in a session variable on which you want to redirect a user. You can write below function in the bootstap file of user module:

protected function _initSubdomainCheck() {
    //Get session in your own way for below line. I'm using action helper to manage the session.
    $userSession = Zend_Controller_Action_HelperBroker::getStaticHelper('siteSession')->getUserSession();
    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
    //check if user is logged in then redirect user to a subdomain
    if(isset ( $userSession->logged_in ) && $userSession->logged_in == 1) {
        //SUBDOMAIN_NAME is a const variable i have set in index.php. If user is already redirected to the subdomain which is stored in session then no need to have further redirection
        if($userSession->urlSubDomainName != SUBDOMAIN_NAME) {
            $redirector->gotoUrl(Zend_Controller_Action_HelperBroker::getStaticHelper('common')->getAfterLoginUrl());
        }
    }
}

Logic behind above function:

  1. Get the session variable
  2. Check whether user is logged in or not
  3. If user is logged in then check if user is already on a subdomain URL, if no then redirect user to a specific sub domain.

I have created an action helper common in /application/helpers/Common.php with below code: Method of this action helper can be called in this way: Zend_Controller_Action_HelperBroker::getStaticHelper('common')->methodName();

class Zend_Controller_Action_Helper_Common extends Zend_Controller_Action_Helper_Abstract{

    public function getSiteUrl(){
        $baseUrl = Zend_Controller_Front::getInstance()->getRequest()->getBaseUrl();
        return $this->getServerProtocol().$_SERVER['HTTP_HOST'] . $baseUrl;
    }

    public function getServerProtocol() {
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
        return $protocol;
    }

    function extract_domain($domain) {
        if(preg_match("/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i", $domain, $matches)) {
           return $matches['domain'];
         } else {
           return $domain;
         }
     }

    public function getSubdomainName() {
        //get session object in your way.
        $session = Zend_Controller_Action_HelperBroker::getStaticHelper('siteSession')->getUserSession();
        $domainName = extract_domain($this->getSiteUrl());
        return $this->getServerProtocol()
            .$session->urlSubDomainName.'.'
            .$domainName;
    }
    public function getAfterLoginUrl(){
        return $this->getSubdomainName().'/yourController/Youraction';
    }

}

Logic for above helper:

  1. We have written this helper to get the subdomain url. First we are getting the subdomain name from the session eg abc
  2. After that extract the domain name. For eg if you have www.example.com then get example.com out of it.
  3. Finally create the subdomain url Protocol + subdomain name + domain name . Eg http://abc.example.com
  4. return this URL.

Hope it helps.

ZF1 has a hostname route type: http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.hostname . The example in the docs covers your exact use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM