Wednesday, May 28, 2014

Yii: Step By Step Create Yiic Console Command Line

This is interesting part in Yii that I like, we can create our own yiic console application to run our function.
http://www.yiiframework.com/doc/guide/1.1/en/topics.console#creating-commands

Step 1: Create Command Class File in path protected/command/shell/commands
class Commands extends CConsoleCommand  {  
    public function actionIndex() {
        echo "Possible action : getMember(), ...\n";
    }
    public function actionMember($id="", $status = "") {
       // action activities
    }  
}

Step 2: Set configuration in /protected/config/console.php
'commandMap' => array(
      'foo' => array(
          'class' => 'application.commands.shell.Commands',
      ),
  ),

Step 3: Test your console command

From command prompt, go to directory protected/ and run yiic. You will notice new command that you have created available on the list. To use your command, just type yiic foo --bar=123 --status=active


1 comment: