cron 的 Slim 3 控制台执行

Slim 3 console execution for cron

我正在尝试创建某种导入来移动数据库信息和转换数据。以后这个 import 需要每天通过 cron 执行。我想使用我编写的部分代码并重用一些模型和控制器。为此,我尝试通过命令行调用 Slim 3,但遇到了一些问题。

控制台命令:

 php cli.php import

我不知道如何正确处理 argv

cli.php:

require __DIR__ . '/vendor/autoload.php';

if (PHP_SAPI == 'cli') {
    $argv = $GLOBALS['argv'];
    array_shift($argv);

    $pathInfo       = implode('/', $argv);

    $env = \Slim\Http\Environment::mock(['PATH_INFO' => $pathInfo]);

    $settings = require __DIR__ . '/app/config/settings.php'; // here are return ['settings'=>'']

    //I try adding here path_info but this is wrong, I'm sure
    $settings['environment'] = $env; 

    $app = new \Slim\App($settings);

    $container = $app->getContainer();
    $container['errorHandler'] = function ($c) {
        return function ($request, $response, $exception) use ($c) {
             //this is wrong, i'm not with http
             return $c['response']->withStatus(500)
                  ->withHeader('Content-Type', 'text/text')
                  ->write('Something went wrong!');
        };
    };

    $container['notFoundHandler'] = function ($c) {
        //this is wrong, i'm not with http
        return function ($request, $response) use ($c) {
            return $c['response']
                ->withStatus(404)
                ->withHeader('Content-Type', 'text/text')
                ->write('Not Found');
        };
    };

    $app->map(['GET'], 'import', function() {
       // do import calling Actions or Controllers
    });
}

如果我执行此操作,我会看到 404 找不到页面 错误。

有什么方向吗?

Slim 3 Framework 论坛有人回答了问题并解决了我的问题。

我只是通过在代码中添加斜杠来避免添加调用来修改它。类似于:

$env = \Slim\Http\Environment::mock(['REQUEST_URI' => '/' . $pathInfo]);

现在代码可以运行了,我可以从命令行调用它了。

php cli.php import

这是因为Slim 3在等待路由,加了斜杠REQUEST_URI我正在模拟,可以执行代码