How to create CRON in zend


It is seen many times that People are struggling to create a Cron.
Creating a cron is not a difficult job. Developer used to struggle about how to set it up.

Cron is nothing but a script which will run on terminal (command prompt)

We will talk about creating a cron using zend framework in php language.

First question arrives in framework:
Generally frameworks have a structure, as zend has its own MVC structure. Now if we want to create a cron for the project which is built in zend, can we use the benefits of a framework? i.e., controller, model, library, helper etc.

The answer is:
Yes, and it is fairly easy in zend.

Lets check how:
Lets assume we already created our module, controller and model and other necessary things to implement certain functionality and already tested using browser.

Now if we directly run the controller from terminal,  it will not work. Because there are no proper bootstrapping done, hence zend will not be able to identify the controller as controller etc.

So, we will create a public php file which will be run in terminal and will bootstrap the zend and call that particular controller.

Lets create cron.php in the public directory of our project,  add put all the necessary constants required to bootstrap. Like-

// Define path to application directory
defined('APP_PATH') || define('APP_PATH', realpath(dirname(__FILE__) . '/../app'));

// Define path to library directory
defined('APP_LIB') || define('APP_LIB', realpath(dirname(__FILE__) . '/../lib'));

// Set application ini path
defined('APP_INI_PATH') || define('APP_INI_PATH', APP_LIB . '/Config/app.ini');

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

// Define application APP_ENV
 defined('APP_ENV') || define('APP_ENV', 'cron-name');

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

// Create application, bootstrap, and run
 $application = new Zend_Application(
 APP_ENV,
 APP_LIB . '/Config/app.ini'
 );
 $application->bootstrap()->run();

Note highlighted APP_ENV, this will be used to call controller.

following code will be in “Config/app.ini” file

['cron-name]
 resources.db.adapter = "pdo_mysql"
 resources.db.params.host = "localhost"
 resources.db.params.username = "root"
 resources.db.params.password = ""
 resources.db.params.dbname = "dbname" 

 resources.frontController.params.prefixDefaultModule = "1"
 resources.frontController.defaultModule         = "yourModuleName"
 resources.frontController.defaultControllerName = "yourControllerName"
 resources.frontController.defaultAction         = "index"

All done. Just type “php cron.php” in your terminal and it will work as usual.

Important notes about cron:

  • Cron should NOT be session protected.
  • browser access of cron file should be restricted. We can achieve this by
        if (PHP_SAPI !== 'cli') {
                echo 'Access Restricted';
                exit;
         }

Hope this will help you building cron while using zend framework. 🙂

Advertisements