6

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 but in Craft. Thank you!

  • What are you hoping to do with Craft once it has been initialized from your other application? – Brandon Kelly Jun 30 '14 at 10:54
  • Using the features in any other system basically. Show entries in Magento for example. – mbalparda Jun 30 '14 at 10:57
  • Is it possible to bootstrap Craft 3.1? I want to upgrade from Craft 2 to Craft 3. Thank you! – Tobias Jan 30 '19 at 15:42
  • Upgrading Craft 2 to 3 is laid out here -- have fun ;): docs.craftcms.com/v3/upgrade.html – narration_sd Jan 30 '19 at 23:09
  • 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 Jan 30 '19 at 23:17
12

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';
| improve this answer | |
  • 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 Jun 30 '14 at 11:24
  • 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 Jun 30 '14 at 11:29
  • 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 Jun 30 '14 at 11:39
  • 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. – Brandon Kelly Jun 30 '14 at 11:42
  • @BrandonKelly Awesome this is exactly what I needed, thanks :) – Lea Hayes Nov 28 '14 at 16:36

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.