【发布时间】:2016-10-08 00:02:57
【问题描述】:
我需要在我的配置文件 (app.php) 中使用来自全局变量 $_SERVER 的一个参数,以便我可以访问 SERVER_NAME 并定义要使用的静态资源服务器。
$staticUrlMap['local.example.com'] = 'localstatic.example.com';
$staticUrlMap['dev.example.com'] = 'devstatic.example.com';
$staticUrlMap['stage.example.com'] = 'stagestatic.example.com';
$staticUrlMap['preprod.example.com'] = 'preprodstatic.example.com';
$staticUrlMap['my.example.com'] = 'static.example.com';
$staticUrl = '';
if(!empty($_SERVER['SERVER_NAME']))
{
$staticUrl = $staticUrlMap[$_SERVER['SERVER_NAME']];
}
return [
'static_url' => $staticUrl,
];
除了在 laravel-config 文件中直接使用 $_SERVER 之外,还有更好的方法吗?
【问题讨论】:
-
使用
$_SERVER有什么问题,事实上我认为它更具可读性,因为我不了解 Laravel。但我已经编写 PHP 近 7 年了,当然知道 $_SERVER 是什么。 P.S - 我真的很讨厌在内置功能周围使用无感知包装器的框架,所以我可能有点偏见。这就是说的一个警告 - 从命令行( CLI )运行 PHP 时,如果记忆对我有用,我认为服务器名称不会存在。 -
你是对的@ArtisticPhoenix,当从 CLI 运行时它不会有 $_SERVER ,这就是为什么我有 !empty 检查以避免在我运行“php artisan”命令时收到警告。自从我进入编程领域已经十年了,但对 Laravel 来说还是新手,我认为接受专家建议并没有什么坏处,所以在这里提出了一个问题。!
-
是的,这是因为 CLI 不在 apache 内部运行,所以没有服务器。很高兴认识你。
-
是的,谢谢。
标签: php laravel laravel-5 config