<p>Craft’s bootstrap file in located at craft/app/index.php. Importing that will get you 99% of the way there.</p>

<p>The only problem is, index.php not only loads Craft, it also runs it. In doing so, Craft is going to insist that it completely resolves the request, with a response to the browser.</p>

<p>I’m thinking we should move most of that code into a new bootstrap.php file, with the only exception being the last line:</p>

<pre><code>$app-&gt;run();
</code></pre>

<p>Then index.php would simply import bootstrap.php and call <code>$app-&gt;run()</code> itself. That way you could import bootstrap.php rather than index.php, and do whatever you want with the app, like <a href="http://buildwithcraft.com/docs/plugins/working-with-elements" rel="nofollow">fetch entries</a>, without losing control over the whole request.</p>

<p>In the meantime, you could copy the contents, sans <code>$app-&gt;run()</code>, of index.php into your own file and import that instead. You’d need to go through it and update all of the paths, too.</p>

<p>Another option would be to make Craft think the page should be a 404, and catch the 404 exception:</p>

<pre><code>// Make it look like a 404
$_GET['p'] = "some/invalid/path";

// Import Craft, catching the 404
try
{
    require 'path/to/craft/app/index.php';
}
catch (\Craft\HttpException $e){}

// ...
</code></pre>

<p>Little hacky, but that may be your cleanest option until we give you a bootstrap.php file to import instead.</p>

<p><strong>UPDATE</strong></p>

<p>As of <a href="http://buildwithcraft.com/updates#build2579" rel="nofollow">Craft 2.2</a>, Craft now ships with a <strong>bootstrap.php</strong> file which handles all of the app initialization stuff with the exception of actually calling <a href="http://www.yiiframework.com/doc/api/1.1/CApplication#run-detail" rel="nofollow">run()</a>.</p>

<p>When you include bootstrap.php(), a new <a href="http://buildwithcraft.com/classreference/etc/web/WebApp" rel="nofollow">Craft application</a> will be returned.</p>

<pre class="lang-php prettyprint-override"><code>$craft = require 'craft/app/bootstrap.php';
</code></pre>

CC BY-SA 3.0
2014-06-30T11:24:34.503Z
I already tried that, by removing the $app->run(); line but it failed. I recreated the craft/app/index.php file in a file called test.php and removed the last line. Error after calling anything is 
Fatal error: Call to undefined function craft()
mbalparda
CC BY-SA 3.0
2014-06-30T11:29:44.453Z
Keep in mind that Craft uses the `Craft` PHP namespace. If your code doesn’t (and it shouldn’t, if it’s outside of Craft), you will need to prepend “Craft\” to all references to Craft’s functions and classes. For example, `$criteria = Craft\craft()->elements->getCriteria(Craft\ElementType::Entry);`
Brandon Kelly
CC BY-SA 3.0
2014-06-30T11:39:42.270Z
Nice, i got it now. Wanna put this somewhere people can reuse it? I can put together some documentation or a small post about it.
mbalparda
CC BY-SA 3.0
2014-06-30T11:42:13.067Z
Might be worth a knowledge base article once we have added the bootstrap.php file. In the meantime, this page certainly suffices -- Stack Exchange has a great built-in search, and they always find their way to the top of Google searches.
Brandon Kelly
CC BY-SA 3.0
2014-11-28T16:36:14.660Z
@BrandonKelly Awesome this is exactly what I needed, thanks :)
Lea Hayes
CC BY-SA 3.0
2014-06-30T11:20:12.997Z
2014-09-02T19:41:04.753Z
2014-09-02T19:41:04.753Z
Brandon Kelly
<p>Is there any known way to bootstrap Craft to use the core functions inside another application? For example, in Magento anyone can include a file, initialize the app and use all the functions from there.
<a href="http://fishpig.co.uk/magento/tutorials/run-magento-code-externally/" rel="nofollow">This is exactly what i am trying to achieve</a> but in Craft.
Thank you!</p>

CC BY-SA 3.0
2014-06-30T10:54:26.963Z
What are you hoping to do with Craft once it has been initialized from your other application?
Brandon Kelly
CC BY-SA 3.0
2014-06-30T10:57:24.347Z
Using the features in any other system basically. Show entries in Magento for example.
mbalparda
CC BY-SA 4.0
2019-01-30T15:42:23.093Z
Is it possible to bootstrap Craft 3.1? I want to upgrade from Craft 2 to Craft 3. Thank you!
Tobias
CC BY-SA 4.0
2019-01-30T23:09:58.457Z
Upgrading Craft 2 to 3 is laid out here -- have fun ;):  https://docs.craftcms.com/v3/upgrade.html
narration_sd
CC BY-SA 4.0
2019-01-30T23:17:23.153Z
Thanks! Upgrading worked fine. But how can I include the bootstrap.php() like in version 2? I want to bootstrap Craft 3 from a Laravel project. `$craft = require 'craft/app/bootstrap.php';`doesn't work anymore.
Tobias
CC BY-SA 4.0
2019-01-31T23:16:24.857Z
Hello, Tobias. You've posted an answer here that's actually a question, so I'm converting it to a comment.
Matt Stein
<config><bootstrap-craft>
Bootstrap Craft from another app
# Bootstrap Craft from another app

(Asked by: username_0 on 2014-06-30)

Is there any known way to bootstrap Craft to use the core functions inside another application? For example, in Magento anyone can include a file, initialize the app and use all the functions from there.
[This is exactly what i am trying to achieve](http://fishpig.co.uk/magento/tutorials/run-magento-code-externally/) but in Craft.
Thank you!

**Tags:** config, bootstrap-craft

> **Comments:**
> **username_1**: What are you hoping to do with Craft once it has been initialized from your other application?  
> **username_0**: Using the features in any other system basically. Show entries in Magento for example.  
> **username_2**: Is it possible to bootstrap Craft 3.1? I want to upgrade from Craft 2 to Craft 3. Thank you!  
> **username_3**: Upgrading Craft 2 to 3 is laid out here -- have fun ;):  https://docs.craftcms.com/v3/upgrade.html  
> **username_2**: Thanks! Upgrading worked fine. But how can I include the bootstrap.php() like in version 2? I want to bootstrap Craft 3 from a Laravel project. `$craft = require 'craft/app/bootstrap.php';`doesn't work anymore.  
> **username_4**: Hello, username_2. You've posted an answer here that's actually a question, so I'm converting it to a comment.  

---

## 1 answers

### Answer by username_1 on 2014-06-30. Score: 12 | Accepted answer

Craft’s bootstrap file in located at craft/app/index.php. Importing that will get you 99% of the way there.
The only problem is, index.php not only loads Craft, it also runs it. In doing so, Craft is going to insist that it completely resolves the request, with a response to the browser.
I’m thinking we should move most of that code into a new bootstrap.php file, with the only exception being the last line:
$app->run();

Then index.php would simply import bootstrap.php and call $app->run() itself. That way you could import bootstrap.php rather than index.php, and do whatever you want with the app, like fetch entries, without losing control over the whole request.
In the meantime, you could copy the contents, sans $app->run(), of index.php into your own file and import that instead. You’d need to go through it and update all of the paths, too.
Another option would be to make Craft think the page should be a 404, and catch the 404 exception:
// Make it look like a 404
$_GET['p'] = "some/invalid/path";

// Import Craft, catching the 404
try
{
    require 'path/to/craft/app/index.php';
}
catch (\Craft\HttpException $e){}

// ...

Little hacky, but that may be your cleanest option until we give you a bootstrap.php file to import instead.
UPDATE
As of Craft 2.2, Craft now ships with a bootstrap.php file which handles all of the app initialization stuff with the exception of actually calling run().
When you include bootstrap.php(), a new Craft application will be returned.
$craft = require 'craft/app/bootstrap.php';

> **Comments:**
> **username_0**: I already tried that, by removing the $app->run(); line but it failed. I recreated the craft/app/index.php file in a file called test.php and removed the last line. Error after calling anything is  Fatal error: Call to undefined function craft()  
> **username_1**: Keep in mind that Craft uses the `Craft` PHP namespace. If your code doesn’t (and it shouldn’t, if it’s outside of Craft), you will need to prepend “Craft\” to all references to Craft’s functions and classes. For example, `$criteria = Craft\craft()->elements->getCriteria(Craft\ElementType::Entry);`  
> **username_0**: Nice, i got it now. Wanna put this somewhere people can reuse it? I can put together some documentation or a small post about it.  
> **username_1**: Might be worth a knowledge base article once we have added the bootstrap.php file. In the meantime, this page certainly suffices -- Stack Exchange has a great built-in search, and they always find their way to the top of Google searches.  
> **username_5**: @BrandonKelly Awesome this is exactly what I needed, thanks :)  

---

