Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cron job with zend framework 2

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?

like image 679
user1409508 Avatar asked Nov 03 '13 10:11

user1409508


People also ask

What is the use of Zend framework in PHP?

Zend Framework is a collection of professional PHP packages with more than 570 million installations. It can be used to develop web applications and services using PHP 5.6+, and provides 100% object-oriented code using a broad spectrum of language features.


2 Answers

I strongly recommend to use CLI for such requirement.

  1. Create a ConsoleController with an updateAction() inside the application module.
  2. 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'
                        )
                    )
                )
            )
        )
    )
    );
    
  3. Now open the terminal and

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

Thats all. Hope it helps.

like image 161
edigu Avatar answered Sep 21 '22 00:09

edigu


I found the solution at collabnet (Which is now dead).

I am copying the solution here as ColabEdit sometimes removes posts:

<?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>
like image 40
HappyCoder Avatar answered Sep 23 '22 00:09

HappyCoder