drevls8138 2011-06-04 14:28
浏览 61
已采纳

使用php导航文件结构以动态创建页面

I am creating quite a complex web application. I like to use php to help reduce the amount of repetitive code that I end up using, creating page plugins if you like.

an example of of a section of code looks like this:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>        
        <?php require_once("root/html_classes/head_links.html"); ?>
        <link href="home/css/home.css" rel="stylesheet" type="text/css" />
        <title>Texation</title>
    </head>

as you can see there is a <?php ?> reference in the above section. As this project is going to be so large I have decided to have an ultra organised file structure for all the pages, scripts, javascript, css ect elements. But it causes me 1 tiny issue.

when I create the modules linked above they themselves have references to images or other scripts and css docs that they use, but this form of inserting html means that all the directory paths need to change for every page they are inserted into, for instance,

when I use them on the index.php page the path to an image in the menu.html would be "root/images/someimage.jpg" but if I use it on the login script the path to the same image would need to be "../images/someimage.jpg"

could anyone suggest a way of making these paths dynamic taking into account the currant position within the directory structure the page they are "inserted" into is ?

I though about using php to build them something like

'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/root/images/someimage.jpg'

but cannot quite get that to work either...

Thank you for your time, any suggestions are very welcome and appreciated.

  • 写回答

2条回答 默认 最新

  • douyuai8994 2011-06-04 14:36
    关注

    If you're talking about the paths to resources, why not just use absolute paths?

    example:

     /root/images/ = http://domain.com/root/images
     root/images = [Current directory]/root/images
    

    the second one needs you to manage updating paths for every file in your site the first one is absolute and works every time.

    If you are having the same problem with your PHP require all you need to use is $_SERVER['DOCUMENT_ROOT'] . '/rest/of/path/from/absolute'

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程