The Rails cli has a runner feature that allows you to run a file (or a string of code) from the command line.

E.g. rails runner my-script.rb (where my-script.rb will run in the context of a fully bootstrapped Rails application). It's a fantastic way to write scripts/backfills/etc without needing to alter your application.

I know that craft includesyiic but it seems to be focused on migrations or code generation and not necessarily providing the same bootstrapped playground.

Is there an easy way to give an arbitrary script access to a bootstrapped craft application? My specific use case is that I want access to models and plugins to be able to migrate fields from one event plugin to another (this is actual content, not just a schema migration).


Maybe you're just looking for a bootstrapped Craft instance?

Something like:

$craft = require '/path/to/craft/app/bootstrap.php';

From there you'll have access to the Craft instance as usual:


Along with your models, records, etc.

  • Ha! What a great and simple solution. I was over complicating things. It looks like yii/craft depends on a bunch of SERVER variables to be defined that are not defined (at least on my environment). I needed to declare SERVER_NAME and REQUEST_URI on $_SERVER to get require to work without throwing an exception. – Nick Tomlin Sep 25 '16 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.