Friday, 10 October 2014

How to set Cron in Zend Framework?

How to set Cron in Zend Framework?

Step 1: Don't execute bootstrap's run function directly in index.php
File Location: public_html/index.php
Update Following code:
if (php_sapi_name() != 'cli' || !empty($_SERVER['REMOTE_ADDR'])) {
 $application->bootstrap()->run();
}


Step 2: Create a folder cronjob in web application root folder.


Step 3. Create a file init.php under cronjob folder and add following code.
// Define path to application directory
defined('APPLICATION_PATH')
        || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
        || define('APPLICATION_ENV', 'environment');

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
            realpath(APPLICATION_PATH . '/../library'),
            get_include_path(),
        )));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
                APPLICATION_ENV,
                APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();



Step 4: Create a file mycron.php under cronjob folder.
Add following code in mycron.php
require_once 'init.php';
print_r($_SERVER);


Step 5. Append following code in mycron.php.
Now, you can access all models like below
$obj = new Application_Model_Logs();
In this file you can access zend's all global variable even you can access all models files with same way you had used in controller.

Step 6: Now Test the cron with following command
Login to putty and go to folder where your PHP is installed and run following code.
php C:\wamp\www\myproject\cronjob\mycron.php