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:
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:
session
eg abc
www.example.com
then get example.com
out of it. Protocol + subdomain name + domain name
. Eg http://abc.example.com
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.