当我将php代码部署到heroku时,我的自动加载器功能无法正常工作。我正在使用名称空间。
它在本地主机中正常工作。我已经对将路径从localhost转换为heroku进行了必要的更改,因为heroku使用/ app作为文档根目录。因此,在以下情况下,BASEURL设置为:
define('BASEURL', $_SERVER['DOCUMENT_ROOT']);
这里是初始化文件的一部分:
spl_autoload_register('myAutoLoaderPerson');
function myAutoLoaderPerson($className) {
$path = BASEURL . '/classes/';
$extension = '.class.php';
$fullPath = $path . $className . $extension;
require $fullPath;
}
我在做什么错?
您确定正确填写了$_SERVER['DOCUMENT_ROOT'])
吗?
我宁愿建议使用一些相对定义来定义BASEURL
,例如,如果Document Root是在定义BASEURL
的文件上方两个文件夹:
define('BASEURL', realpath(__DIR__ . "/../../"));
或简化自动装带器,使其相对于路径:
spl_autoload_register('myAutoLoaderPerson');
function myAutoLoaderPerson($className) {
require_once __DIR__ . "/../../../classes/$className.class.php";
}
您建议返回/app/classes/lib\foo.class.php
。
注意/
和\
的混合。可能是不同之处在于您在本地windows上,但是在远程linux上。
如果遵循PSR-4约定,则意味着您的名称空间应与目录匹配,但是为此,您可能需要将\
转换为/
。
也许像这样:
spl_autoload_register('myAutoLoaderPerson');
function myAutoLoaderPerson($className) {
require_once __DIR__ . "/../<path..to>/classes/" . strtr($classname, "\\", "/") . ".class.php";
}