将php网站部署到heroku中的自动加载器问题

问题描述 投票:0回答:1

当我将php代码部署到heroku时,我的自动加载器功能无法正常工作。我正在使用名称空间。

File structure

Heroku log

它在本地主机中正常工作。我已经对将路径从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;
}

我在做什么错?

php class heroku autoloader
1个回答
0
投票

您确定正确填写了$_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

注意/\的混合。可能是不同之处在于您在本地上,但是在远程上。

如果遵循PSR-4约定,则意味着您的名称空间应与目录匹配,但是为此,您可能需要将\转换为/

也许像这样:

spl_autoload_register('myAutoLoaderPerson');

function myAutoLoaderPerson($className) {
    require_once __DIR__ . "/../<path..to>/classes/" . strtr($classname, "\\", "/") . ".class.php";
}


© www.soinside.com 2019 - 2024. All rights reserved.