Symfony 动态子域

发布于 2024-09-04 08:08:12 字数 348 浏览 4 评论 0原文

我正在尝试将子域与 symfony 中的客户 ID 进行匹配。

即我有 customer1.example.com 和 customer2.example.com

域存储在表中。

当用户访问 customer1.example.com 时,我想获取子域,在数据库中查找域名,一旦匹配,它将为该客户部署应用程序配置,然后将 customer_Id 存储在全局属性中所以我清楚地知道在整个申请过程中我正在与哪个客户打交道。虚拟主机将具有相关的通配符服务器名称。

您是否成功实现了这一目标?如果是,是如何实现的?如果没有,任何想法都会有很大帮助!

我正在考虑使用过滤器来做到这一点。

:-)

I'm trying to match subdomains to a customer id in symfony.

i.e. i have customer1.example.com and customer2.example.com

Domains are stored in a table.

When a user goes to customer1.example.com, I would like to get the subdomain, look up the domain name in the database, once matched, it will then deploy the app config for that customer and then store the customer_Id in a global attribute so i know exactly which customer I'm dealing with througout the whole application. The virtual host will have the relevant wildcard servername.

Have you managed to achieve this, and if so, how? If not, any ideas would be a great help!

I'm thinking about using a filter to do it.

:-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人接听 2024-09-11 08:08:12

还需要将您的域设置为通配符域,如果不需要,您将需要为每个客户端手动创建每个子域。

另一个不太依赖 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>

该代码基本上会将子域、域和请求的页面发送到请求的页面。然后在 php 中你可以检查它是否等于你的客户端用户名。并允许您同时为您的客户使用托管域名。

我希望它有帮助。

also going to be needing yo set your domain as a wildcard domain, if not you going to need to create manually each subdomain per client.

another solution that is not so symphony dependent is using a .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>

that code basically will send to the requested page the subdomain, the domain and the page requested. then in php you can check if it is equal to your client username. and allow you also to use parked domains for your clients at the same time.

i hope it helps.

骷髅 2024-09-11 08:08:12

看看 sfDomainRoutePlugin - 它可以满足您的需求。但是,在当前版本中,您无法获得 Propel 或 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);
}

这只是一个示例,但我自己使用类似的方法,并且该插件执行广告中的操作。祝你好运。

如果你喜欢冒险,你可以看看《More with symfony book》的第二章。这将帮助您理解 sfDomainRoutePlugin 中的代码。

Take a look at sfDomainRoutePlugin - it does what you want. However, in its current version you don't get the Propel or DoctrineRoute functionality, which means you must manually lookup the customer based on the subdomain parameter returned from the plugin. Example:

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

This is just an example, but I use a similar approach myself, and the plugin does what is advertised. Good luck.

If you're adventurous, yuo could take a look at Chapter 2 in the "More with symfony book". This would help you understand the code in sfDomainRoutePlugin.

如若梦似彩虹 2024-09-11 08:08:12

因为您想加载不同的应用程序,所以过滤器无济于事。只需使用前端控制器(index.php)提取子域,如果应用程序目录存在,则加载应用程序(否则404)。您甚至可以将 ID 存储在应用程序配置中。

Because you want to load different app, filter won't help. Just use the frontcontroller (index.php) to extract the subdomain, and if the app directory exists, load the app (else 404). You can even store the id in app configuration.

野稚 2024-09-11 08:08:12

我正在做类似的事情。请注意,我还没有尝试过这个确切的设置。

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

I'm doing something similar. Note, I haven't tried this exact setup.

$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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文