我正在zend Framework 1.12上创建一个应用程序,我有一个用户模块,该模块接受来自“ www.example.com”的用户登录和注册。验证用户后,我要将用户重定向到用户特定的子域“ username.example.com”。我已经配置了我的主机文件和apache的vhost文件。
在这方面的任何帮助,不胜感激。
提前致谢。
这是我在项目中用于多个子域的方式;
在index.php
文件中提取子域名(如果存在),并将其设置为常量。稍后将使用。
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);
后login
保存在您希望将用户重定向会话变量的子域的名称。您可以在模块bootstap
文件中编写以下函数user
:
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());
}
}
}
以上功能的逻辑:
我已经创建了一个动作助手common
在/application/helpers/Common.php
与下面的代码:这个动作助手的方法可以以这种方式被称为: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';
}
}
以上帮手的逻辑:
session
如abc
www.example.com
那就example.com
摆脱它。Protocol + subdomain name + domain name
。例如http://abc.example.com
希望能帮助到你。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句