spl_autoload_register 发现是自己搞错了

forwebreg

在config/app.php上加了spl_autoload_register 但是好像并没有什么用


        spl_autoload_register(function ($class) {
            $filePath = '/' . str_replace('\\', '/', $class) . '.php';
            if (file_exists(base_path(false) . $filePath)) {
                require_once base_path(false) . $filePath;
            }
        });
368 2 0
2个回答

北月妖王

事实上是可以的,可能你的文件路径不对,可以调试输出一下路径。

北月妖王

截图

截图

截图

  • forwebreg 2025-05-31

    用的是我的这个spl_autoload_register吗
    我现在是外部的能加载到 反而内部的会报不存在
    Error: Class "app\common\resource\lib\Dingtalk" not found in phar:///app/webmam.bin/app/common/queues/consumerPush/PushDingMsg.php:22

  • forwebreg 2025-05-31

    外部文件的命名空间是namespace branchs\xxx这样啊 没有用app的

  • 北月妖王 2025-05-31

    服了。
    spl_autoload_register(function ($class) {
    $filePath = '/' . str_replace('\', '/', $class) . '.php';
    if (file_exists(base_path(false) . $filePath)) {
    echo $filePath . PHP_EOL;
    require_once base_path(false) . $filePath;
    }
    });

  • forwebreg 2025-05-31

    外部能echo出来的 现在是被打包的内部的引用报错了 。。。

  • 北月妖王 2025-05-31

    如果需要验证文件是否已经被打包进 phar 中,可以自己解包。

    <?php
    $pharFile = __DIR__ . '/webmam.phar'; // 你可以根据需要修改路径
    // 检查文件是否存在
    if (!file_exists($pharFile)) {
    die("错误:Phar 文件不存在:$pharFile\n");
    }

    try {
    $phar = new Phar($pharFile);
    } catch (Exception $e) {
    die("错误:无法打开 Phar 文件:{$e->getMessage()}\n");
    }

    $targetDir = __DIR__ . '/extrac';

    echo "开始解包 Phar 文件到:$targetDir\n";

    try {
    $phar->extractTo($targetDir);
    echo "解包完成。\n";
    } catch (Exception $e) {
    echo "解包失败:{$e->getMessage()}\n";
    }

  • forwebreg 2025-05-31

    我去 真的是没打包进去 我根目录下有个resource 然后app\xxx\下也有resource console的exclude_pattern里加了|/resource/ 我以为只会屏蔽根目录下的resource 刚才才发现是正则匹配全部

  • forwebreg 2025-05-31

    感谢大佬

  • 北月妖王 2025-05-31

    人才啊

  • forwebreg 2025-05-31

    给大佬看笑话了 哈哈

🔝