Welcome Guest, Not a member yet? Register   Sign In
Running CI without a web server (e.g., for cron scripts or unit tests)
#1

[eluser]Unknown[/eluser]
I've created a little script which bootstraps the CI superobject and loader, allowing one to use CI outside a cgi/mod_php context and without a Controller object.

This has been especially handy for cronjobs and unit tests.

Since I take a different, more direct approach than what I saw on the wiki, I've posed my code here. Note that my approach does not require a web server or even php-cgi at all.

To use it, modify the CI_REALBASE constant and $system_folder and $application_folder variables to point to your real CI install. Then simply require_once the fake-codeigniter.php file at the top of your script, then use get_instance() to access and start using the CI superobject.

Additional notes:
* The script assumes the use of PHP5. PHP4 will require modifications (you'll have to check the php version and load Base4 instead of Base5).
* As little as possible is loaded, and the autoloader is not run, so you will have to load a lot of things yourself.
* This was written for CI 1.6.3, but will probably work with 1.7.0 with little or no modification.
* It makes some simplifying assumptions, so you may have to tweak things further if you have an unusual install.

Code:
<?php
/**
* Set up required variables for testing CI libraries, etc WITHOUT running codeigniter.
*
* @author Francis Avila
**/

/**
**/
error_reporting(E_ALL);

define('CI_VERSION',    '1.6.3');

define('CI_REALBASE', dirname(__FILE__).'/../htdocs/');

$system_folder = CI_REALBASE."system";

define('EXT', '.php');
define('BASEPATH', $system_folder.'/');

$application_folder = 'application';
define('APPPATH', BASEPATH.$application_folder.'/');

// These are needed just so the URI class doesn't choke.
// Their value does not really matter, but will affect
// what the url helper functions produce.
$_SERVER['SERVER_NAME'] = 'mydomain.net';
$_SERVER['SERVER_PORT'] = '80';

require(BASEPATH.'codeigniter/Common'.EXT);
require(APPPATH.'config/constants'.EXT);
require(BASEPATH.'codeigniter/Base5'.EXT);

$LANG =& load_class('Language');
$CFG =& load_class('Config');
$URI =& load_class('URI');

class CI_Fake extends CI_Base
{
    function __construct()
    {
        parent::CI_Base();
        // This is just to get around the fact that the $instance static var
        // of the CI_Base class object is private.
        $this->load   = & load_class('Loader');
        $this->config = & load_class('Config');
        $this->uri    = & load_class('URI');
    }
}
new CI_Fake();


Messages In This Thread
Running CI without a web server (e.g., for cron scripts or unit tests) - by El Forum - 01-20-2009, 09:34 AM



Theme © iAndrew 2016 - Forum software by © MyBB