You can run your CakePHP from your cron or the command line.

In many times you need to run CakePHP programs with your command line or your cron for your purposes. Such as:

1.You need to backup your customer datas with php language or CakePHP Framework.
2.You need to create other command line programs with php language or CakePHP Framework.

If you write your programs with basic php , not problem. But if you use CakePHP you can do these:

1.Download CakePHP version 1.3.1(I guaranteed this version).
2.Copy file APP/webroot/index.php and change this file name to be shell.php(or other name if you want) put it to same place.
3.Open file shell.php, to go the last line and you will see these lines:

   if (isset($_GET['url']) && $_GET['url'] === 'favicon.ico') {
      return;
   } else {
      $Dispatcher = new Dispatcher();
      $Dispatcher->dispatch();
   }

You may change to be

   // Dispatch the controller action given to it
   // eg php cron_dispatcher.php /controller/action
   define('CRON_DISPATCHER', true);
   if($argc >= 2) {
      $Dispatcher= new Dispatcher();
      $Dispatcher->dispatch($argv[1]);
   }

4.Open your terminal command line, change directory to APP/webroot/ and run this command:

php shell.php /mycontroller/myaction

mycontroller is your custom controller
myaction is your custom action


5.This step let you create a controller and place in APP/controllers/. Now I named it mycontroller_controller.php.For it's contents are about these:

<?php
class MycontrollerController extends AppController {
   var $name = 'Mycontroller';
   var $uses = array();
   var $view = 'none'; //you can use this line in many purposes
   function myaction() {
      echo "Hello World\n";
   }
}
?>


6.Last step,In cases that you may be want to create other view types, you can use this method.

In this case, I don't need to use any view or template, notice variable $view value(none) on above.Now let you bring following codes to create a file in name none.php and place it in directory CAKEPHP/cake/libs/view/

<?php
App::import('View', 'View', false);

class NoneView extends View {
   var $_headers = array();
   function __construct(&$controller) {
      parent::__construct($controller);
   }
   function render() {
      return;
   }
}
?>


OK complete,please happy for this trip.

Good Luck.
@enjoyman
Comments