Skip to content

Instantly share code, notes, and snippets.

@sany2k8
Created April 13, 2017 09:38
Show Gist options
  • Select an option

  • Save sany2k8/1e7b7a58b5eb96b2dc0d1b3a16ae1fac to your computer and use it in GitHub Desktop.

Select an option

Save sany2k8/1e7b7a58b5eb96b2dc0d1b3a16ae1fac to your computer and use it in GitHub Desktop.

Yii command line script

For default (No action exists)

  1. For connection establishment, copy contents of /protected/config/main.php to /protected/config/console.php with DB connection & other information
  2. Comment this line //'defaultController' => 'controller_name' (if_exists)
  3. Then create a file inside commands folder with any name you wish as per this convention yournameCommand.php e.g /protected/commands/SiteCommand.php
  4. Paste below code in SiteCommand.php File which extends from CConsoleCommand and have name SiteCommand
<?php
       // it will run default if not action defined in the on the SiteCommand class
        class SiteCommand extends CConsoleCommand
        {
          public function run($args)
          {
              echo 'Hello yii friends from command line';
          }
        }
?>
  1. Then open cmd.
  2. Go to cd \path\protected>yiic then press Enter Key.
  3. The cmd will show you the available commands names.
  4. Run ./yiic site and then press Enter Key
  5. It will Show your output "Hello yii friends from command line".

For user defined controller action

  1. Follow above steps 1,2,3 and copy below code
  2. Follow above steps 5,6,7
  3. Finally run this command to show output $ ./yiic MyController MyActionName

<?php
        class MyControllerCommand extends CConsoleCommand
        {
          public function actionMyActionName(){
          
            echo 'Hello yii friend from controller action'; 
          
          }
        }
?>

  1. It will Show your output "Hello yii friend from controller action".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment