I need to use one parameter from global variable $_SERVER
in my config file (app.php), so that I can access SERVER_NAME
and define which static resource server to use.
$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,
];
Is there a better way to achieve this other than using $_SERVER
directly in laravel-config file?
You may use the Request
façade to access the $_SERVER
super-global.
echo Request::server('SERVER_NAME');
As an opinion side note, I generally agree with @ArtisticPhoenix's comment about readability and distaste for framework wrappers around basic functionality. However, using the wrappers for super-globals makes it a lot simpler to unit test. Also, of little relevant importance, there are some popular style guides out there that will fail you for direct access of the super-globals.