<p>Maybe you're just looking for a bootstrapped Craft instance?</p>

<p>Something like:</p>

<pre><code>$craft = require '/path/to/craft/app/bootstrap.php';
</code></pre>

<p>From there you'll have access to the Craft instance as usual:</p>

<pre><code>$craft-&gt;entries-&gt;saveEntry();
</code></pre>

<p>Along with your models, records, etc.</p>

CC BY-SA 3.0
2016-09-25T15:06:29.257Z
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
CC BY-SA 3.0
2016-09-25T03:53:42.373Z
2016-09-25T03:53:42.373Z
Brad Bell
<p>The Rails cli has a <a href="http://guides.rubyonrails.org/command_line.html#rails-runner" rel="nofollow"><code>runner</code></a> feature that allows you to run a file (or a string of code) from the command line. </p>

<p>E.g. <code>rails runner my-script.rb</code> (where <code>my-script.rb</code> 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.</p>

<p>I know that craft includes<code>yiic</code> but it seems to be focused on migrations or code generation and not necessarily providing the same bootstrapped playground.</p>

<p>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). </p>

<database><yii>
Craft equivalent to Rails runner
# Craft equivalent to Rails runner

(Asked by: username_0 on 2016-09-24)

The Rails cli has a [`runner`](http://guides.rubyonrails.org/command_line.html#rails-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 includes`yiic` 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).

**Tags:** database, yii

---

## 1 answers

### Answer by username_1 on 2016-09-25. Score: 2 | Accepted answer

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:
$craft->entries->saveEntry();

Along with your models, records, etc.

> **Comments:**
> **username_0**: 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.  

---

