我有这样的文件夹
\htdocs\router\index.php -> main index file
\htdocs\router\view\ -> all html and php files
\htdocs\router\view\asset -> all css, js , etc files
\htdocs\router\controller\App\main.php -> controller file
现在有人指向http://localhost时,index.php从main.php(类main)创建新对象(new main()
)并执行索引函数(public function index() { ... }
)。这个函数有这样的包含(require 'view/login.php';
)。
我可以看到login.php的输出但是所有.css,.jpg,.js文件都不会加载,因为它们在login.php中声明如下:
<link rel="stylesheed" href="asset/style.css" type="text/css">
因为asset/
目录位于login.php
文件所在的位置。
如果我让href="view/asset/style.css"
正常工作。
问题是:
如何在其他文件夹中包含.php文件,以便在不修改href
标记且不移动资源文件夹的情况下使其工作。
答案 0 :(得分:1)
实际上,如果不改变href
,则无法使其工作,因为资产是指根,而不是路径。
您可以做的是将view/asset
的符号链接设为root,因此可以从href="asset/style.css"
和href="view/asset/style.css"
访问资源。
使用PHP:
symlink("view/asset","asset"); //index.php
或终端:
ln -s view/asset asset