0

例如,我正在处理 2 个项目 A 和 B,它们将来都将托管在根目录中。Atm 我有一个带有默认参数的 apache web 服务器,我想开始工作,所以我创建了文件夹 A 和文件夹 B,我将在其中存储所有需要的文件。

问题是我什么时候会使用类似的东西

<script type="text/javascript"  src="/resources/scripts/javascript.js" ></script>
<link  href="/resources/styles/stylesheet.css" rel="stylesheet" type="text/css" />

这两个项目都将从 apache 根文件夹 (httpdocs) 调用此文件,而不是从 /a/ 和 /b/ 文件夹。

当我使用 PHP 框架时情况变得更糟,所以我不能只使用

<link  href="./resources/styles/stylesheet.css" rel="stylesheet" type="text/css" />

(/resources/ 之前的点)因为路由项目试图像文件夹一样访问控制器并从那里获取资源。

听起来像一个小问题,但我无法弄清楚并为自己解决这个问题,所以每次我需要开发一些东西时,我都会一次又一次地发明解决方法:)

4

2 回答 2

1

您可以在每个项目文件夹中创建一个 .htaccess 文件(如果您无权访问 httpd.conf)

ScriptAlias /resources/  "/var/www/html/resources/"
<Directory "/var/www/html/resources/">
   #... whatever you want to set here ...
</Directory>

然后就可以继续使用了

script type="text/javascript"  src="/resources/scripts/javascript.js" ></script>
<link  href="/resources/styles/stylesheet.css" rel="stylesheet" type="text/css" />
于 2013-08-28T12:15:55.887 回答
1

A good way of handling this is to use a constant that holds the path to your project. That way you only have to edit that constant to get your paths right.

define('BASEDIR', '/a/');
echo '
<script type="text/javascript"  src="'.BASEDIR.'resources/scripts/javascript.js" ></script>
<link href="'.BASEDIR.'resources/styles/stylesheet.css" rel="stylesheet" type="text/css" />';

This will turn your paths into

<script type="text/javascript"  src="./a/resources/scripts/javascript.js" ></script>
<link href="./a/resources/styles/stylesheet.css" rel="stylesheet" type="text/css" />';

If you keep that constant in a seperate file you can include it where you'd need it. A php file that handles an AJAX request for instance.

于 2013-08-28T12:06:43.120 回答