【问题标题】:How to run cron job with zend framework 2如何使用 zend 框架 2 运行 cron 作业
【发布时间】:2016-08-18 06:40:52
【问题描述】:

我在 Zend Framework 2 中构建了应用程序。我想设置 cron 作业来更新我的产品。我知道这样的脚本应该从公共文件夹之外运行,但不幸的是我在 cron 中的脚本需要使用框架文件。
我该怎么做?
我想出的唯一方法是从公用文件夹外部运行脚本,然后添加一些哈希或密码并重定向到

www.domain.com/cron/test

所以我将拥有所有框架功能。
会安全吗?也许还有其他方法?

【问题讨论】:

    标签: php cron zend-framework2


    【解决方案1】:

    我强烈建议使用 CLI 来满足此类要求。

    1. 在应用程序模块内创建一个带有 updateAction() 的 ConsoleController。
    2. console route 添加到您的应用程序模块的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'
                          )
                      )
                  )
              )
          )
      )
      );
      
    3. 现在打开终端并

      $ cd /path/to/your/project
      $ php public/index.php updateproducts
      

    仅此而已。希望对您有所帮助。

    【讨论】:

    • 谢谢,我试试。就一个问题。你的方法安全吗? HTTP 无法访问它?
    • 是的,HTTP 无法访问它。来自文档:"Console Routes will only be processed when the application is run inside console (terminal) window. They have no effect in web (http) request and will be ignored. It is possible to define only HTTP routes (only web application) or only Console routes (which means we want a console-only application which will refuse to run in a browser)." 查看:framework.zend.com/manual/2.2/en/modules/…
    • 谢谢,我想确定一下。我会在检查后接受你的回答。大概就是明天吧。感谢您的帮助!
    【解决方案2】:

    我在 collabnet 找到了解决方案(现在已经死了)。

    我在这里复制解决方案,因为 ColabEdit 有时会删除帖子:

    <?php
    /*
    Cron directory setup:
    
    Cron
        config
            module.config.php
        src
            Cron
                Controller
                    IndexController.php
        autoload_classmap.php
        Module.php                
    
    NOTES: Remember to include the Cron module in the main config file (trunk/config/application.config.php)
    
    Once you have the route in place, write your cron and call it from your webhost cron manager.
    
    */
    
    // Cron/config/module.config.php
    return array(
        // Placeholder for console routes
        'controllers' => array(
            'invokables' => array(
                'Cron\Controller\IndexController' => 'Cron\Controller\IndexController'
            ),
        ),
        'console' => array(
            'router' => array(
                'routes' => array(
                    //CRON RESULTS SCRAPER
                    'my-first-route' => array(
                        'type'    => 'simple',       // <- simple route is created by default, we can skip that
                        'options' => array(
                        'route'    => 'hello',
                        'defaults' => array(
                            'controller' => 'Cron\Controller\IndexController',
                            'action'     => 'index'
                            )
                        )
                    )
    
                ),
            ),
        ),
    
    
    );
    
    <?php
    // Cron/src/Cron/Controller/IndexController.php
    namespace Cron\Controller;
    
    use Zend\Mvc\Controller\AbstractActionController;
    
    class IndexController extends AbstractActionControlle
    {
        public function indexAction()
        {
            echo "hello";
            echo "\r\n";
        }
    }
    
    From the console navigate to trunk (or public_html) (the directory before public) and run:
    
    path/to/trunk>php public/index.php hello
    
    hello
    path/to/trunk>
    

    【讨论】:

    • 以上链接已失效!
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2010-10-23
    • 1970-01-01
    • 2012-06-03
    • 2010-10-05
    • 1970-01-01
    • 2016-06-14
    • 2018-08-06
    相关资源
    最近更新 更多