Implementing and executing cron jobs
Sometimes, an application requires some background tasks such as re-generating a sitemap or refreshing statistics. A common way to implement this is by using cron jobs. When using Yii, there are two ways to do this, which are as follows:
Emulate the browser to call the web application controller action
Use the command-line command to run as a job
In this recipe, we will see how to implement both. For our recipe, we will implement writing the current timestamp into a timestamp.txt file under the protected directory.
Getting ready
Create a fresh application by using yiic webapp.
How to do it...
Carry out the following steps:
Create
protected/controllers/CronController.phpas follows:<?php class CronController extends CController { public function actionIndex() { $filename = Yii::getPathOfAlias("application")."/timestamp.txt"; file_put_contents($filename, time()); } }Now we need a way to call it. As it is a web application controller, we need to...