我的app结构如下
application -> views -> templates
// some more files
page.php
-> controllers
home.php
-> models
items.php
router.php
index.php
第一种情况:
的index.php
include 'application/routes.php';
Routes.php
require "controllers/home.php";
控制器/ home.php
require '/application/models/clusters.php'; //works
require 'application/models/clusters.php'; //works
require '../models/clusters.php'; //doesn't work
为什么第一行有效但不是最后一行?
第二种情况:
的index.php
include 'application/views/page.php';
page.php文件
glob("application/views/templates/*.php") // array of files
glob("templates/*.php") // empty array
我认为我对php中的路径如何工作的理解有问题,但我无法弄清楚它是什么。有时路径似乎是相对于当前脚本,相对于index.php而言相对于其他时间,但不一定与我使用/
答案 0 :(得分:5)
请试试这个:
require 'application/controllers/home.php';
require 'application/models/clusters.php';
问题是PHP中的路径始终相对于第一个文件路径,在本例中为index.php。所以你必须包含目录'application'。
另一种方法是使用set_include_path:http://php.net/manual/pt_BR/function.set-include-path.php
修改强>
查看include_path:
echo ini_get('include_path');
答案 1 :(得分:1)
我总是惊讶于有多少人不理解include_path的重要性。
除非您为include / require指定了绝对路径(即以/开头的路径),否则PHP会使用include_path通过依次处理每个包含路径条目来尝试找到该文件。通常,包含路径中的第一个条目是。表示当前目录(如果在脚本中的该点执行,则由getcwd()返回。
答案 2 :(得分:-4)
尝试这个
require '/../models/clusters.php'; //doesn't work