Wednesday, November 24, 2010

Codeigniter - How to invoke Controller from command line

So I was digging around for a way to invoke one of my Codeigniter(CI) controllers from the command line. This was the solution I liked the most.

Create the following file in the root CI folder of your web server e.g. /srv/www/htdocs/

I called mine test.php. Also make the file executable (chmod a+x).



#!/usr/bin/php
/* make sure this isn't called from a web browser */
if (isset($_SERVER['REMOTE_ADDR'])) die('Permission denied.');

/* set the controller/method path */
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'] = '_PATH_TO_CONTROLLER_';

/* raise or eliminate limits we would otherwise put on http requests */
set_time_limit(0);
ini_set('memory_limit', '256M');

/* call up the framework */
include(dirname(__FILE__).'/index.php');
?>





Now just replace _PATH_TO_CONTROLLER_ with the path to the controller:

e.g. /armaudit/update_ad_names_res

Enjoy!

No comments:

Followers