問題描述
我想找到我的應用程序的基本 url,以便我可以自動引用我的應用程序樹中的其他文件...
I'd like to find the base url of my application, so I can automatically reference other files in my application tree...
因此,在我的應用程序的基礎中給定一個文件 config.php,如果子目錄中的文件包含它,則知道在 url 前添加什么前綴.
So given a file config.php in the base of my application, if a file in a subdirectory includes it, knows what to prefix a url with.
application/config.php
application/admin/something.php
application/css/style.css
因此,鑒于訪問了 http://www.example.com/application/admin/something.php
,我希望它能夠知道 css 文件在 中$approot/css/style.css
.在這種情況下,$approot
是/application
",但我希望它知道該應用程序是否安裝在其他地方.
So given that http://www.example.com/application/admin/something.php
is accessed, I want it to be able to know that the css file is in $approot/css/style.css
. In this case, $approot
is "/application
" but I'd like it to know if the application is installed elsewhere.
我不確定這是否可能,許多應用程序(我認為是 phpMyAdmin、Squirrelmail)必須首先設置一個配置變量.如果它只是知道它會更加用戶友好.
I'm not sure if it's possible, many applications (phpMyAdmin, Squirrelmail I think) have to set a config variable to begin with. It would be more user friendly if it just knew.
推薦答案
我在 homebrew 框架中使用以下內容...將其放在應用程序根文件夾中的文件中并簡單地包含它.
I use the following in a homebrew framework... Put this in a file in the root folder of your application and simply include it.
define('ABSPATH', str_replace('\', '/', dirname(__FILE__)) . '/');
$tempPath1 = explode('/', str_replace('\', '/', dirname($_SERVER['SCRIPT_FILENAME'])));
$tempPath2 = explode('/', substr(ABSPATH, 0, -1));
$tempPath3 = explode('/', str_replace('\', '/', dirname($_SERVER['PHP_SELF'])));
for ($i = count($tempPath2); $i < count($tempPath1); $i++)
array_pop ($tempPath3);
$urladdr = $_SERVER['HTTP_HOST'] . implode('/', $tempPath3);
if ($urladdr{strlen($urladdr) - 1}== '/')
define('URLADDR', 'http://' . $urladdr);
else
define('URLADDR', 'http://' . $urladdr . '/');
unset($tempPath1, $tempPath2, $tempPath3, $urladdr);
上面的代碼定義了兩個常量.ABSPATH 包含應用程序根目錄(本地文件系統)的絕對路徑,而 URLADDR 包含應用程序的完全限定 URL.它確實適用于 mod_rewrite 情況.
The above code defines two constants. ABSPATH contains the absolute path to the root of the application (local file system) while URLADDR contains the fully qualified URL of the application. It does work in mod_rewrite situations.
這篇關于如何找到應用程序的基本網址?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!