dongshungai4857 2013-08-28 12:01
浏览 19
已采纳

子文件夹中的Web项目开发

For example i am working on 2 projects, A & B, both of them in future will be hosted in root directories. Atm i have one apache web server with default parameters and i want to start working, so i create folder A and folder B, where i will store all needed files.

The problem is when i ll use something like

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

both of the projects will call this files from apache root folder (httpdocs), not from /a/ and /b/ folders.

Things get worse when i am using PHP framework, so i cant just use

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

(dot before /resources/) because with routing project is trying to access controller like a folder and get resources from there.

Sounds like a small problem, but i cant figure it out and close this question for myself, so each time i need to develop something i invent workarounds again and again :)

  • 写回答

2条回答 默认 最新

  • duancan65665 2013-08-28 12:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类