Php Symfony动态子域
我正在尝试将子域与symfony中的客户id进行匹配 i、 e.我有customer1.example.com和customer2.example.com 域存储在一个表中 当用户访问customer1.example.com时,我希望获得子域,在数据库中查找域名,一旦匹配,它将为该客户部署应用程序配置,然后将客户Id存储在全局属性中,这样我就可以在整个应用程序中准确地知道我正在与哪个客户打交道。虚拟主机将具有相关的通配符servername 你有没有做到这一点,如果有,如何做到?如果没有,任何想法都会大有帮助 我正在考虑用一个过滤器来做这件事Php Symfony动态子域,php,symfony1,filter,subdomain,Php,Symfony1,Filter,Subdomain,我正在尝试将子域与symfony中的客户id进行匹配 i、 e.我有customer1.example.com和customer2.example.com 域存储在一个表中 当用户访问customer1.example.com时,我希望获得子域,在数据库中查找域名,一旦匹配,它将为该客户部署应用程序配置,然后将客户Id存储在全局属性中,这样我就可以在整个应用程序中准确地知道我正在与哪个客户打交道。虚拟主机将具有相关的通配符servername 你有没有做到这一点,如果有,如何做到?如果没有,任何
:-)因为您想加载不同的应用程序,筛选器将不起作用。只需使用frontcontroller(index.php)来提取子域,如果应用程序目录存在,则加载应用程序(else 404)。您甚至可以在应用程序配置中存储id。我正在做类似的事情。注意,我还没有尝试过这种精确的设置
$tokens = explode('.', $_SERVER['SERVER_NAME'], 2);
$app = $tokens[0] == 'www' ? 'default' : $tokens[0]; //assumes you aren't allowing www.app.example.com, change if you are
try
{
$appConfiguration = ProjectConfiguration::getApplicationConfiguration($app, 'prod', false);
}
catch(InvalidArgumentException $e) //thrown if app doesn't exist
{
$fallbackConfiguration = ProjectConfiguration::getApplicationConfiguration('default', 'prod', false);
$context = sfContext::createInstance($fallbackConfiguration);
$request = $context->getRequest();
$request->setParameter('module', 'default'); //set what route you want an invalid app to go to here
$request->setParameter('action', 'invalidApplication');
$context->dispatch();
}
if (isset($appConfiguration))
{
sfContext::createInstance($appConfiguration)->dispatch();
}
看一看-它做你想要的。但是,在其当前版本中,您没有获得推进或DoctrineRoute功能,这意味着您必须根据插件返回的子域参数手动查找客户。例如:
app/frontend/config/routing.yml
# pick up the homepage
homepage:
url: /
class: sfDomainRoute
param: { module: homepage, action: index }
requirements:
sf_host: [www.example.com, example.com]
# catch subdomains for customers
customer_subdomain:
url: /
class: sfDomainRoute
param: { module: customer, action: index }
app/frontend/modules/customer/actions.class.php
public function executeIndex(sfWebRequest $request)
{
// get the subdomain parameter
$this->subdomain = $request->getParameter('subdomain');
// retrieve customer (you have to create the retrieveBySubdomain method)
$this->customer = CustomerPeer::retrieveBySubdomain($this->subdomain);
}
这只是一个例子,但我自己也使用了类似的方法,插件做了广告中所宣传的事情。祝你好运
如果你喜欢冒险,yuo可以看看“更多关于symfony的书”中的第2章。这将帮助您理解sfDomainRoutePlugin中的代码。还需要将您的域设置为通配符域,否则需要为每个客户端手动创建每个子域 另一个不太依赖symphony的解决方案是使用.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) $1?sub=%2&page=$1&domain=%{HTTP_HOST} [QSA,L]
<IfModule>
选项+FollowSymLinks
选项+索引
重新启动发动机
重写基/
重写cond%{HTTP_HOST}!www.domain.com$[NC]
重写cond%{HTTP_HOST}^(www.)?([a-z0-9-]+).domain.com[NC]
重写规则(.*)$1?sub=%2&page=$1&domain=%{HTTP_HOST}[QSA,L]
该代码基本上会将子域、域和请求的页面发送到请求的页面。然后在php中,您可以检查它是否等于您的客户端用户名。并允许您同时为您的客户使用驻留的域
希望有帮助。谢谢!希望有人知道答案!:-)嘿,马瑞克。我没有部署不同的应用程序,我只想使用sudomain作为参数变量。例如:我希望每个客户都有相同的应用程序,而不是有,但我使用子域来获取公司的相关数据。昨晚我成功了。我创建了一个过滤器(domainMatchFilter.class.php)。我抓取了子域,在customer表上搜索以匹配域(表中的domain字段上有一个索引)。一旦找到,它将使用结果集中的customer对象设置一个配置变量。i、 e.sfConfig::set(“客户”,$customer)。如果没有找到客户,它将重定向到主页(example.com)。你认为最好重定向到一个页面,上面写着这个客户不存在等等。?这个过滤器会在每个页面请求上运行,速度有点慢,因为它每次都会查询数据库,但是,这是我知道的唯一方法。我希望它像uservoice.com一样工作。你注册了,你就拥有了自己的域名,比如company1.uservoice.com等等。这比uservoice.com/company1好多了。好吧,我误解了。filter方法是正确的方法,不必担心额外的查询。对于可用性,你应该说这个客户并不存在。