1

I'm using Craft Pro 2.3.2625

I'm trying to write a command-line called script which can call Craft's functions (to create new entries with assets).

Based on Brandon's updated answer here:

Bootstrap Craft from another app

Using:

$craft = require 'craft/app/bootstrap.php';

in a standalone script should just work after Craft 2.2 - but doing so gives me the following errors (paths tweaked to remove project name):

PHP Notice:  Undefined index: SERVER_NAME in craft/app/bootstrap.php on line 105

Notice: Undefined index: SERVER_NAME in craft/app/bootstrap.php on line 105

Notice: Undefined property: Craft\WebApp::$request in craft/app/controllers/TemplatesController.php on line 156

Fatal error: Call to a member function isSiteRequest() on a non-object in craft/app/controllers/TemplatesController.php on line 156

1 Answer 1

3

That bootstrap file is only meant to be used by web applications. Even if you got past this error, the file is still going to give you a WebApp instance, when what you really want to be working with in that environment is a ConsoleApp.

If you want to load Craft from the command line, use craft/app/etc/console/yiic instead (make sure it has executable permissions), or if you already have a PHP script running, load yiic.php.

Unfortunately those files don’t work like bootstrap.php though - they will not only instantiate a ConsoleApp instance, they’ll run it as well. So you would need to provide your own functionality through a plugin that adds custom console commands. You would do that by creating a consolecommands folder in your plugin folder, with files inside it that end with Command.php (e.g. MyPlugin_UtilsCommand.php) and a matching class name, which extends BaseCommand.

More details about how to write the class are available from BaseCommand’s class reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.