我可以使用 silex 获取请求的域吗

Can I get the domain of a request with silex

我想创建一个 silex 应用程序来监听到两个域的路由请求。根据域的不同,响应应该不同。 我知道我可以简单地注入一个 Request 对象并使用它来获取主机,但我想使用更好的方式,如 this,但使用 Silex。

这可能吗?如果可能:如何?

它应该像为所有路由调用 host() method for each route (you can also do that on an entire ControllerCollection instance 一样简单)像这样:

<?php

// initialization, etc.

$app->get('/', function() {
  return new Response("Only from my.host.com!");
})
->host('my.host.com')
;

这不是一个糟糕的问题 - 我最终来到这里是为了自己寻找一个快速的答案。

事实证明这很容易 - 您只需要您的 Symfony Request 实例即可访问以下方法:

$base_url = $request->getHost();
$app['monolog']->notice('Base path result', ['base_url' => $base_url])

我的结果:

[2016-02-19 10:04:15] api.INFO: 基本路径结果[] {"base_url":"api-test.xonadu.com"}

希望对您有用:)