Php 如何访问常量文件laravel中的$服务器[';服务器名称';]
我想访问laravel常量文件中的服务器名称,但我面临Php 如何访问常量文件laravel中的$服务器[';服务器名称';],php,laravel,global-variables,Php,Laravel,Global Variables,我想访问laravel常量文件中的服务器名称,但我面临未定义索引:server\u name,运行此条件时会出现错误 已定义常量APIRL 这是我的constant.php if((isset($_SERVER['SERVER_NAME'])) && ($_SERVER['SERVER_NAME'] == 'onboarding.posplus.co.za')){ define('APIURL', 'https://example.com/bib/public/api
未定义索引:server\u name
,运行此条件时会出现错误
已定义常量APIRL
这是我的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');
请帮帮我
谢谢您应该首先检查应用程序是从控制台运行还是通过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');
}
测试此代码:
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";
}
$\u服务器
(和其他相关变量)仅在请求期间存在。@Amadan但如何在常量中使用,因为我想在服务器名称上添加条件,并为qa和生产运行urlserver@Amadan我想在Q.A服务器和生产服务器上放置相同的文件,并在条件基础上运行。明白了吗?您可以改为使用。当我运行命令php artisan config:cache时,它会给出错误常量APIRL已经定义了我已经在Constant.php文件中测试过了,它的给定错误类请求没有exist@RaheelAslam您正在使用此代码的laravel的哪一部分?@RaheelAslam laravel的一部分,如控制器、视图?不是laravel versioni,我在中使用constant.php文件laravel@RaheelAslam好的,请将文件的所有内容粘贴到您的帖子中