Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T11:24:34.503Z
Answers.Text: 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()
Answers.UserDisplayName: mbalparda
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T11:29:44.453Z
Answers.Text: 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);`
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T11:39:42.270Z
Answers.Text: 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.
Answers.UserDisplayName: mbalparda
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T11:42:13.067Z
Answers.Text: 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.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-28T16:36:14.660Z
Answers.Text: @BrandonKelly Awesome this is exactly what I needed, thanks :)
Answers.UserDisplayName: Lea Hayes
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-06-30T11:20:12.997Z
Answers.LastActivityDate: 2014-09-02T19:41:04.753Z
Answers.LastEditDate: 2014-09-02T19:41:04.753Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-30T10:54:26.963Z
Comments.Text: What are you hoping to do with Craft once it has been initialized from your other application?
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-06-30T10:57:24.347Z
Comments.Text: Using the features in any other system basically. Show entries in Magento for example.
Comments.UserDisplayName: mbalparda
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-30T15:42:23.093Z
Comments.Text: Is it possible to bootstrap Craft 3.1? I want to upgrade from Craft 2 to Craft 3. Thank you!
Comments.UserDisplayName: Tobias
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-30T23:09:58.457Z
Comments.Text: Upgrading Craft 2 to 3 is laid out here -- have fun ;):  https://docs.craftcms.com/v3/upgrade.html
Comments.UserDisplayName: narration_sd
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-30T23:17:23.153Z
Comments.Text: 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.
Comments.UserDisplayName: Tobias
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-31T23:16:24.857Z
Comments.Text: Hello, Tobias. You've posted an answer here that's actually a question, so I'm converting it to a comment.
Comments.UserDisplayName: Matt Stein
Tags: <config><bootstrap-craft>
Title: Bootstrap Craft from another app
ThreadText: # 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](http://buildwithcraft.com/docs/plugins/working-with-elements), 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](http://buildwithcraft.com/updates#build2579), Craft now ships with a **bootstrap.php** file which handles all of the app initialization stuff with the exception of actually calling [run()](http://www.yiiframework.com/doc/api/1.1/CApplication#run-detail).
  
  When you include bootstrap.php(), a new [Craft application](http://buildwithcraft.com/classreference/etc/web/WebApp) will be returned.
  
  ```php
  $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**: @username_1 Awesome this is exactly what I needed, thanks :)  
  
  ---
  
  