TP5设置类似TP3常量 __ROOT__ 的函数

介绍了在TP5下获取类似TP3中__ROOT__函数的方法,该方法返回的结果roo,本地项目返回./你的项目/,线上项目返回./ 。还给出了在本人测试项目中,入口文件index.php和config.php中的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用该方法可在TP5下获取到类似TP3中__ROOT__的函数。
返回的结果roo:
本地项目返回:./你的项目/
线上项目返回:./

function root(){
	$root  = $phpfile = '';
	$iscgi = (0 === strpos(PHP_SAPI,'cgi') || false !== strpos(PHP_SAPI,'fcgi')) ? 1 : 0 ;
	if ( $iscgi ) {
		$temp    = explode('.php',$_SERVER['PHP_SELF']);
		$phpfile = rtrim(str_replace($_SERVER['HTTP_HOST'],'',$temp[0].'.php'),'/');
	} else {
		$phpfile = rtrim($_SERVER['SCRIPT_NAME'],'/');
	}
	$root =   rtrim(dirname($phpfile),'/');
	return (($root=='/' || $root=='\\') ? '' : $root);
}

以上转载自:http://www.thinkphp.cn/code/3475.html

在本人测试项目中使用:
1、入口文件index.php中写入以下代码

// 获取项目地址
$root  = $phpfile = '';
$iscgi = (0 === strpos(PHP_SAPI,'cgi') || false !== strpos(PHP_SAPI,'fcgi')) ? 1 : 0 ;
if ( $iscgi ) {
	$temp    = explode('.php',$_SERVER['PHP_SELF']);
	$phpfile = rtrim(str_replace($_SERVER['HTTP_HOST'],'',$temp[0].'.php'),'/');
} else {
	$phpfile = rtrim($_SERVER['SCRIPT_NAME'],'/');
}
$root =   rtrim(dirname($phpfile),'/');
$root = ($root=='/' || $root=='\\') ? '' : $root;

// [ 应用入口文件 ]
// 定义应用目录
define('APP_PATH', __DIR__ . '/application/');
// 静态文件目录
define('PUBILC_PATH', $root.'/public/');
// 上传文件目录
define('UPLOAD_PATH', $root.'/public/uploads');
// 根目录
define('ROOT',$root);
// 加载框架引导文件
require __DIR__ . '/thinkphp/start.php';

2、config.php中

return [
	// 视图输出字符串内容替换
    'view_replace_str' => [
		'__ROOT__'=>ROOT
	]
]
import os import sys from pathlib import Path root_dir = str(Path(__file__).parent.parent.parent.parent.parent) if root_dir not in sys.path: sys.path.insert(0, root_dir) from model.controllertestcase import ControllerTestCase from OmadaControllerAPI.OpenAPIService.dashboard_service import DashboardService import random import string import time class MyTestCase(ControllerTestCase): def __init__( self, omadacloud_url: str, controller_url: str, username: str, password: str, environment: str, region: str, force_login: bool = False, ): super().__init__( omadacloud_url, controller_url, username, password, environment, region, force_login, ) self.init_case_name_id(os.path.basename(__file__)) self.item_id = "检查/openapi/v1/{omadacId}/sites/{siteId}/dashboard/channels接口" self.expected_result = "Get channel distribution and usage succeed" def setup(self): self.check_test_times() self.get_cookie_and_token_from_config() self.login_by_openapi(type=0) self.dashboard_service = DashboardService( self.openapi_host, self.openapi_headers, self.logger ) def test(self): try: response = self.dashboard_service.get_channel_distribution_and_usage( self.omadacId, 'test-id' ) response_json = response.json() if response_json["errorCode"] == 0: self.case_result = self.result.PASS self.actual_result = "Get channel distribution and usage succeed" self.logger.info(self.actual_result) else: self.case_result = self.result.FAIL self.actual_result = ( f"Get channel distribution and usage failed: {response_json['msg']}" ) self.logger.error(self.actual_result) except Exception as e: self.case_result = self.result.FAIL self.actual_result = f"Get channel distribution and usage failed: {e}" self.logger.error(self.actual_result) self.add_result( self.case_result, self.item_id, self.actual_result, self.expected_result, "", ) def teardown(self): pass if __name__ == "__main__": testcase = MyTestCase( "", "https://localhost:8043/82064d2c4dff19907fd0fe09bf88ef8d", "admin", "Tplink123!", "prd", "aps1", True, ) testcase.do_test()
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值