欢迎各位兄弟 发布技术文章
这里的技术是共享的
在 Drupal 7 中,获取网站的根路径(根目录或根URL)可以通过以下几种方法实现:
这是Drupal核心文件所在的文件系统路径(如/var/www/drupal7
或C:\xampp\htdocs\drupal7
)。
✅ 方法 1:使用 DRUPAL_ROOT
常量
php$root_directory = DRUPAL_ROOT; // 返回Drupal安装的绝对路径
✅ 方法 2:使用 drupal_get_path('root')
php$root_dir = drupal_get_path('root'); // 也可获取根目录
✅ 方法 3:从 $_SERVER
获取
php$root = realpath($_SERVER['DOCUMENT_ROOT']); // 可能不准确,取决于服务器配置
这是网站的完整URL(如http://example.com
或http://example.com/subdir
)。
✅ 方法 1:使用 $base_url
全局变量
phpglobal $base_url; $site_url = $base_url; // 返回 http://example.com 或 http://example.com/subdir
✅ 方法 2:使用 base_path()
函数
php$base_path = base_path(); // 返回相对路径,如 "/" 或 "/subdir/"
✅ 方法 3:使用 url()
函数
php$home_url = url('<front>', array('absolute' => TRUE)); // 返回首页绝对URL
DRUPAL_ROOT
返回的是文件系统路径(适用于文件操作)。
$base_url
返回的是完整的网站URL(适用于生成链接)。
获取文件系统根目录 → DRUPAL_ROOT
获取网站URL根路径 → $base_url
或 base_path()
需要根据具体需求选择合适的方法!