Cron的Slim 3控制台执行 [英] Slim 3 console execution for cron

查看:74
本文介绍了Cron的Slim 3控制台执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create some kind of import to move database info and transform data. In the future this import needs to be executed by cron every day. I want to use part of my written code and reuse some models and controllers. To do this I'm trying to call Slim 3 through the command line, but I have some problems.

控制台命令:

 php cli.php import

我不知道如何处理 argv

I don't know how to process argv correctly.

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页面未找到错误。

If I execute this I see a 404 Page not found error.

有什么方向吗?

推荐答案

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

Someone in Slim 3 Framework discussion forums answered the question and solved my problem.

我刚刚通过在代码中添加斜线来避免添加调用来对其进行了修改。

I just modified it by adding a slash in the code to avoid adding a call. Something like:

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

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

Now the code works and I can call it from the command line.

php cli.php import

这是因为Slim 3等待路由,并在我正在仿真的REQUEST_URI中添加斜线,然后我可以执行代码。

This is because Slim 3 waits for a route, and adds a slash to REQUEST_URI I'm emulating and I can execute code.

这篇关于Cron的Slim 3控制台执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆