如何在常量文件laravel中访问$ _SERVER ['SERVER_NAME']

时间:2018-07-12 05:41:40

标签: php laravel global-variables

我想访问laravel常量文件中的服务器名称,但是我正面临Undefined index: SERVER_NAME,在运行此条件时会出现错误

  

已经定义了恒定的APIURL

这是我的constant.php

 if((isset($_SERVER['SERVER_NAME'])) && ($_SERVER['SERVER_NAME'] == 'onboarding.posplus.co.za')){

    define('APIURL', 'https://example.com/bib/public/api/v1/'); 
    define('BIBURL','https://example.com/bib/public/assets/');
    define('IS_VERIFY',false);
    define('BUSINESS_URL','https://example.com/api/app/business/validate');
    define('X_API_KEY','234242');
    define('SECRET','dsadad');
    define('IS_HTTPS',FALSE);
    define('DOMAIN','pro');
}else{

    define('APIURL', 'https://example.com/public/bib/public/api/v1/');
    define('BIBURL','https://example.com/public/bib/public/assets/');
    define('IS_VERIFY',true);
    define('BUSINESS_URL','example.com/api/v1/app/business/validate');
    define('X_API_KEY','sdsadss');
    define('SECRET','67889999999');
    define('IS_HTTPS',TRUE);
    define('DOMAIN','qa');
}


define('USERNAME','1n@.com');

请帮助我。

谢谢

2 个答案:

答案 0 :(得分:2)

您应该首先检查应用程序是从控制台运行还是通过http请求运行:

if (!app()->runningInConsole()) {
    // not running in console, must be a request...

    $name = request()->server('SERVER_NAME');

    // your code here ...

} else {
    // console invoked (e.g. artisan)

    $name = gethostname(); // or php_uname('n') for older PHP versions

    // your code here ...
}

关于APIURL被定义的错误,您需要首先检查是否已定义,然后再定义:

if (!defined('APIURL')) {
    define('APIURL', 'http://example.com');
}

答案 1 :(得分:0)

测试此代码:

if(request()->server('SERVER_NAME') == 'onboarding.posplus.co.za')){

    if (!defined('apiurl')) {
        define('apiurl', "https://example.com/bib/public/api/v1/'");
    }

    $biburl = "https://example.com/bib/public/assets/";
    $isVerify = false;
    $businessUrl = "https://www.example.com/api/app/business/validate";
    $x_api_key   =  "234324";
    $secret   =  "sdfsfsdf";
    $isHttp   =  FALSE;
    $domain   =  "pro";

}else{

    if (!defined('apiurl')) {
        define('apiurl', "https://example.com/bib/public/assets/");
    }

    $biburl = "https://example.com/bib/public/assets/";
    $biburl = "https://example.com/public/bib/public/assets/";
    $isVerify = true;
    $businessUrl = "https://example.com/api/v1/app/business/validate";
    $x_api_key   =  "q12313";
    $secret   =  "123123asdasd";
    $isHttp   =  TRUE;
    $domain   =  "qa";

}