【问题标题】:Difficulties with alternate routing替代路由的困难
【发布时间】:2017-01-26 07:31:41
【问题描述】:

我正在为自己开发一个 PHP 项目。我的文件夹结构是这样的:

index.php
head.php
config
    config.php (database connection)
src
    the other php files (ie. nav.php, stats.php)
helpers
    the "oncerunning scripts" (ie. login.php, logout.php)
css
    style.css

在 index.php 的开头以及 src 和 helpers 目录中的几乎每个 php 中,我都包含了“head.php”。如果我运行 index.php,一切正常,但如果我登录它会导致 WSOD,因为 login.php 获取 head.php (../head.php),但 head.php 不给它配置文件的路径(它转发说config.php在config/目录下,但是helpers目录下没有config目录)

我该如何调试?

【问题讨论】:

    标签: php


    【解决方案1】:
    1. 尝试使用绝对路径而不是相对路径, 这是因为相对路径是根据执行的 php 脚本调用的。
    2. 另一种选择是允许一个脚本管理所有进程;例如文件:index.php

      <?php
      include_once('config/config.php');
      ...//various configurations
      if ($state == $login)
      {
          include_once('src/login.php');
      }
      else if ($state == $stats)
      {
          include_once('src/stats.php');
      }
      

    那么你只需要根据一个php脚本配置相对路径,在本例中是脚本index.php

    【讨论】:

    • 谢谢。我应该将主文件放在与 index.php 相同的目录中吗?
    • 没有必要。如果您使用 index.php 作为主要执行点,则路径可以相对于 index.php。或者您可以在每个子目录中有一个文件,在需要时获取所有必要的配置,这对于配置文件和类文件尤其重要。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多