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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘