我的app结构如下
application -> views -> templates
// some more files
page.php
-> controllers
home.php
-> models
items.php
router.php
index.php
第一种情况:
索引.php
include 'application/routes.php';
路由.php
require "controllers/home.php";
Controller /home.php
require '/application/models/clusters.php'; //works
require 'application/models/clusters.php'; //works
require '../models/clusters.php'; //doesn't work
为什么第一行有效而最后一行无效?
第二种情况:
索引.php
include 'application/views/page.php';
页面.php
glob("application/views/templates/*.php") // array of files
glob("templates/*.php") // empty array
我认为我对路径在 php 中的工作方式的理解有问题,但我无法弄清楚它是什么。有时路径似乎是相对于当前脚本的,有时是相对于 index.php 的,但不一定与我用 /
最佳答案
请试试这个:
require 'application/controllers/home.php';
require 'application/models/clusters.php';
问题是 PHP 中的路径总是相对于第一个文件路径,在本例中是 index.php。因此,您必须包含“应用程序”目录。
另一种方法是使用 set_include_path:http://php.net/manual/pt_BR/function.set-include-path.php
编辑
查看您的 include_path:
echo ini_get('include_path');
关于php - 为什么这些相对路径在 php 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350020/