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!
1 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';
-
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() Commented Jun 30, 2014 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);
Commented Jun 30, 2014 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. Commented Jun 30, 2014 at 11:39
-
1Might 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. Commented Jun 30, 2014 at 11:42
-
@BrandonKelly Awesome this is exactly what I needed, thanks :) Commented Nov 28, 2014 at 16:36
$craft = require 'craft/app/bootstrap.php';
doesn't work anymore.