Slim 3 控制台执行 cron

Mor*_*ork 2 php console cron command-line-interface slim

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

控制台命令:

 php cli.php import
Run Code Online (Sandbox Code Playgroud)

我不知道如何正确处理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
    });
}
Run Code Online (Sandbox Code Playgroud)

如果执行此命令,我会看到404 页面未找到错误。

有什么指示吗?

Mor*_*ork 5

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

我只是通过在代码中添加斜杠来修改它以避免添加调用。就像是:

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

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

php cli.php import
Run Code Online (Sandbox Code Playgroud)

这是因为 Slim 3 等待路由,并向我正在模拟的 REQUEST_URI 添加斜杠,这样我就可以执行代码。