Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel 5.x配置文件中访问PHP超级全局变量的最佳方法_Php_Laravel_Laravel 5_Config - Fatal编程技术网

在Laravel 5.x配置文件中访问PHP超级全局变量的最佳方法

在Laravel 5.x配置文件中访问PHP超级全局变量的最佳方法,php,laravel,laravel-5,config,Php,Laravel,Laravel 5,Config,我需要在配置文件(app.php)中使用全局变量$\u SERVER中的一个参数,以便访问SERVER\u NAME并定义要使用的静态资源服务器 $staticUrlMap['local.example.com'] = 'localstatic.example.com'; $staticUrlMap['dev.example.com'] = 'devstatic.example.com'; $staticUrlMap['stage.example.com'] = 'stagestatic.exa

我需要在配置文件(app.php)中使用全局变量
$\u SERVER
中的一个参数,以便访问
SERVER\u 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配置文件中使用
$\u服务器
之外,还有更好的方法实现这一点吗?

您可以使用
请求
外观访问
$\u服务器
超级全局

echo Request::server('SERVER_NAME');

作为意见补充,我大体上同意@ArtisticPhoenix关于可读性的评论,并且不喜欢围绕基本功能的框架包装。然而,对超级全局函数使用包装器使单元测试变得简单得多。另外,没有什么重要意义,有一些流行风格的指南会让你无法直接访问超级全局。使用
$\u服务器有什么问题,事实上,我认为它更可读,因为我不认识拉威尔。但我编写PHP已经将近7年了,我当然知道什么是$\u服务器。另外,我真的很讨厌在内置功能周围使用无传感器包装的框架,所以我可能有点偏颇。这说明了一个警告-当从命令行(CLI)运行PHP时,如果内存允许,我认为服务器名称不会存在。你说得对@ArtisticPhoenix,当从CLI运行时,它不会有$\u服务器,这就是为什么我有!空检查以避免在运行“php artisan”命令时收到警告。我已经十年没有从事编程工作了,但对拉威尔来说还是个新手,我觉得接受专家的建议并没有什么坏处,所以我在这里提出了一个问题。!是的,这是因为CLI没有在apache内部运行,所以没有服务器。很高兴认识你,这是真的,谢谢。