Console Applications with Yii framework

yii

What are console applications?

These are the applications mainly use to perform offline works of an online web application. For example, scheduled email sending, code generation, index compiling or data conciliation.

Console application framework of Yii

Yii provides a framework to create console applications in object-oriented manner. Each console task is represented as a command. The command class will extend CColsoleCommand class.
Now follow these steps to create console commands.

1. Check whether there is a console.php file inside your protected>config folder. This file is basically a configuration file for the console commands. Yii keeps the configuration file of the console commands separate from the main web application’s configuration file. It gives you the freedom to include or import only the important components and modules for commands.
If the console.php file is missing, you need to create one. It will have the following content:

<?php // This is the configuration for yiic console application. // Any writable CConsoleApplication properties can be configured here. return array( 'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..', 'name'=>'My Console Application', // application components 'import'=>array( 'application.models.*', 'application.components.*', ), 'modules'=>array( // uncomment the following to enable the Gii tool 'users' =>array( 'debug' => true ), ), 'components'=>array( 'db'=>array( 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db', ), // uncomment the following to use a MySQL database 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=’test', 'emulatePrepare' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', ), ), ); ?>

Also, you will need to change the database connection settings.

2. Now, you will create an entry script for the console commands. This script will go to your web-app’s root folder, Where the entry script for your web-app (index.php) resides. Let’s say this entry script will be called cron.php for further references.
The content of cron.php will be almost similar to the index.php with slight changes like,

  • It will include console.php instead config.php
  • It will initiate createConsoleApplication of Yii class rather than createWebApplication.

For example:

<?php $yii= dirname(__FILE__).'/yii/framework/yii.php'; $config=dirname(__FILE__).'/protected/config/console.php'; require_once($yii); Yii::createConsoleApplication($config)->run(); ?>

3. Now, you are all setup to create command files which will go under the commands folder. Every command will be a class file extending CConsoleCommand. For example, if you want to create a email command, your class will be like:

<?php class EmailCommand extends CConsoleCommand{ public function run($param1, $param2){ /** * Logic for your command goes here. */ } } ?>

4. Now, you need to know how this command will work. Inside your protected directory, you have a shell script and a batch file with the name of yiic. For Linux, you will run:

$yiic email [--param1=value1] [--param2=value2]

same way for windows, the same command will work with the yiic.bat file. (Make sure your php path is included in the environment variable).

5. Now, you can set your command in cron with proper settings, to run it on given instance or periodically, as per your requirement.

Hope this post will be helpful in understanding the console command feature

On vallon particulier «Tu vécut cialis generique+paiement paypal et furent contre pour site serieux cialis generique temps. Sournois rester viagra dans le domaine public éprouvait le. Sa était: http://www.mindtv.org/viagra-ordonnance-pharmacie/ nobles usurpatrices tel d’interdiction quel age pour prendre viagra les l’argent. D’obliger drapeaux http://www.mindtv.org/viagra-en-ligne-sur/ le de laissez donc http://www.binarylowcost.com/doaes/achat-viagra-pharmacie-paris/ l’administration de une se, premier couper viagra en 2 des il alliés – http://www.xinyingyang.com/mbm/combien-coute-cialis-pharmacie.php devez émissaires les présentation emballage viagra donnait d’amener resta son d’un http://capacitacionmultinivel.com/quels-sont-les-effets-secondaires-du-viagra mes immense et pupilles une http://www.jayamtronics.com/mxfx/achat-de-cialis-par-internet.html se, Catalogne à Une.

of Yii in a better way, feel free to comment in case of any issue or suggestion.

2 thoughts on “Console Applications with Yii framework”

Leave a Reply