如何使用zend框架运行cron作业2 [英] How to run cron job with zend framework 2
问题描述
我有应用程序内置在Zend Framework 2.我想设置cron作业更新我的产品。我知道这样的脚本应该从公共文件夹外部运行,但不幸的是我的脚本在cron需要使用框架文件。
我该如何做呢?
唯一的方法我想出的是从公共文件夹外部运行脚本,然后添加一些哈希或密码,并重定向到
I have application built in Zend Framework 2. I would like to set cron job for updating my products. I know scripts such as this should be run from outside of public folder, but unfortunately my script in cron needs to use framework files.
How can I do this?
The only way I figured out is to run script from outside of public folder then add some hash or password and redirect to
www.domain.com/cron/test
所以我将拥有所有的框架功能。
安全?也许还有其他方法?
So I will have all framework functionality.
Will it be secure? Maybe there is a other way?
推荐答案
我强烈建议使用CLI这样的要求。
I strongly recommend to use CLI for such requirement.
- 在应用程序模块中创建一个带有updateAction()的ConsoleController。
-
添加脚本路由到您的应用程序模块的
module.config.php
:
- Create a ConsoleController with an updateAction() inside the application module.
Add a console route to your application module's
module.config.php
:
array(
'router' => array(
'routes' => array(
...
)
),
'console' => array(
'router' => array(
'routes' => array(
'cronroute' => array(
'options' => array(
'route' => 'updateproducts',
'defaults' => array(
'controller' => 'Application\Controller\Console',
'action' => 'update'
)
)
)
)
)
)
);
现在打开终端
Now open the terminal and
$ cd /path/to/your/project
$ php public/index.php updateproducts
这些都是。希望它有帮助。
Thats all. Hope it helps.
这篇关于如何使用zend框架运行cron作业2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!