![](/img/trans.png)
[英]What is the correct way to set the session duration for continuing it in PHP?
[英]What is the right way to set the correct path in PHP?
我正在使用PHP MVC框架,而我剛剛發現的問題是目錄中特定文件的路徑。 當我路由到呈現頁面的函數時,未加載模板文件中定義的所有文件,包括CSS和圖像。
這是我的文件夾結構
首先,我使用以下代碼在index.php中設置路徑,它是前端控制器:
<?php
// Path to system
$system_path = str_replace('\\', '/', dirname(__FILE__)).'/system';
$template_path = $system_path.'/templates';
?>
與其他任何php框架一樣,我可以在整個框架中使用這些路徑變量。 因此,在我的家庭模板文件(system / templates / home / home.php)中,我有一個標簽來調用位於system / templates / home / css / style.css中的style.css。 所以我在模板文件中的鏈接標簽是
<link rel='stylesheet' type='text/css' href='<?= $template_path; ?>/home/css/style.css' />
當我渲染頁面時,上面的css文件的絕對路徑正確顯示為
C:/Appserv/www/mysite/system/templates/home/css/style.css
但是樣式表從未加載過,我也不知道為什么。 當我單擊指向CSS文件的鏈接時,它會返回403錯誤,並帶有奇怪的路徑,如下所示
You don't have permission to access /mysite/C:/AppServ/www/mysite/system/templates/home/css/style.css
這使我感到困惑,因為此時路徑不正確,因為它以網站名稱開頭,后跟上面模板文件中定義的路徑。
反正有解決這個問題的方法嗎?
將CSS文件加載到網頁中時,不需要系統文件路徑,但需要URL。 所以代替:
<link rel='stylesheet' type='text/css' href='<?= $template_path; ?>/home/css/style.css' />
您需要類似:
<link rel='stylesheet' type='text/css' href='/home/css/style.css' />
或者,如果您定義了基本URL:
<link rel='stylesheet' type='text/css' href='<?= $base_url; ?>/home/css/style.css' />
如果仍然要訪問本地文件,請考慮改用file:///
協議(不推薦)。
我認為最好在index.php或init.php中定義“ template_url”或“ base_url”常量:
define('TEMPLATES_URL','http://localhost/system/templates');
然后像這樣在您的html中使用它:
<link rel='stylesheet' type='text/css' href='<?php echo TEMPLATES_URL; ?>/home/css/style.css' />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.