【发布时间】:2016-08-24 10:51:52
【问题描述】:
我正在尝试创建某种导入来移动数据库信息和转换数据。将来这个导入需要每天由 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 Page not found 错误。
有什么路线吗?
【问题讨论】:
-
我建议使用 symfony/console,它会有点独立,但我将它用于所有长时间运行的任务和 CLI 操作,因为它简单且易于插入。
-
谢谢,我想但我想重用我所有的项目类来完成任务,并且 symfony/console 与 Slim PSR-7 不兼容。
标签: php cron console command-line-interface slim