cron的Slim 3控制台执行


Slim 3 console execution for cron

我正在尝试创建某种导入来移动数据库信息和转换数据。将来,这个导入需要由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中添加了一个斜杠,我可以执行代码。