Bootstrapping ExpressionEngine to integrate with other scripts


Very often I’ve had to have external scripts access the ExpressionEngine environment, including the database, the sessions, and whatever else. For example, a cron job, or just simple one off scripts. So there are two ways most people do this, both are annoying. The simplest, is to create a template with PHP enabled. This is annoying because of all the extra steps involved, plus you get the overhead of extra database queries. The second is to create a module action. This is even more annoying, less database overhead, but you have to go through extra loops of creating a module and installing it. Plus you get to work with meaningless URL like http://mysite.com?ACT=324.

For EE1, I had distilled out all the code necessary to instantiate all the global EE objects. This was a one time annoyance, and I had to, unfortunately, duplicate a lot (read:all) the code, but it has come in handy many times.

Recently, I had to do this for EE2 as well, and it was not so straightforward.

Of course I began by distilling index.php and CodeIgniter.php, duplicating all that code. The only tricky parts, are that you need a fake “controller”.

I handled this by creating a small class, instantiating it:

class A extends Controller {}
$EE = new A;

Once I got to that point, all I had to do was call:

$EE->core->_initialize_core();

Now you can integrate EE anywhere, including any other systems or frameworks:

$system_path = '/path/to/system';
include 'boostrap-ee2.php';

And we have our $EE super global god object.

The full code is hosted on Github:

,

  1. #1 by Ivan at February 23rd, 2011

    I have been looking for a bootstrap file for EE 2.x for a while.
    I have tried using it but I can’t get it to work.
    With which version of EE did you try it?
    Is there anything else I need to keep in mind?

    Thank you in advance!

    • #2 by Avi at February 23rd, 2011

      What exactly isn’t working about for you?

  2. #3 by Mark Bowen at February 23rd, 2011

    You mention above about doing this for ExpressionEngine 1. I was just wondering how much work was involved exactly?

    It’s just that I may need to do something akin to this on a 1.0 website and would love to know how you went about doing that.

    Best wishes,

    Mark

    • #4 by Avi at February 23rd, 2011

      Wasn’t a lot of work. Just distilling and copying out code from core.system.php.

  3. #5 by Tim at February 24th, 2012

    Hey – this looks like a great script. Unfortunately I couldn’t get it working with EE 2.4 – tried fudging it myself by looking through the newer codeigniter.php file – wondered if you knew whether there had been any major changes or even better if you had an updated version? ;)

(will not be published)