在后台运行脚本(shell_exec)在作曲家/PHP-DI函数上失败



我手动运行该脚本,一切正常,

$container = require '../config/bootstrap.php';
    try {
       $test  = $container->get(Test::class);
       $test->run();
    }
    catch(Exception $e) {
        echo  $e->getMessage() . "n";
        die;
    }

但是当我从另一个文件夹调用该文件时,如下所示:

$script = __DIR__ .'/folder/test-service.php';
$output = shell_exec('php '.$script);
var_dump($output);

我收到以下错误:

致命错误:require((:需要打开失败需要"../config/bootstrap.php' (include_path='.:'( 在/otherFolder/folder/test-service.php 第 13 行

在那之后,我收到了很多文件不存在的错误。

引导.php包括 PHP-DI 的类映射

需要"../vendor/autoload.php';

use DIContainerBuilder;
$containerBuilder = new ContainerBuilder;
$containerBuilder->addDefinitions(__DIR__ . '/config.php');
$container = $containerBuilder->build();
return $container;

require指令相对于当前目录。

您可以将该行替换为require __DIR__.'/../config/bootstrap.php';

最新更新