问题
I'm writing an import script to add some external content to a C5 installation.
I'm unsure which file(s) I need to include in order to get the C5 API available (without any action like loading a page)?
/concrete/dispatcher.php
does a truckload of initializations, but I'm unsure which ones are required. Just including dispatcher.php
causes the script to navigate to the installation dialog.
Is there a canonical way to initialize C5 in an external tool? I can't find any in the developer docs.
回答1:
There is a C5_ENVIRONMENT_ONLY
constant you can define, after which you can include the "index.php" file to get the c5 system without having it serve a specific page or theme.
<?php
define('C5_ENVIRONMENT_ONLY', true);
include("index.php");
//C5 should be loaded now, so you can interact with the API...
I just tested this out on the latest version (5.6.1) and it worked just fine -- I was able to query the database via Loader::db(), and able to interact with logged-in user object (global $u). So I assume if those 2 things worked then anything would work.
I did find these 2 forum threads from a while ago:
- http://www.concrete5.org/developers/bugs/5-4-2-1/c5_integration-with-upgraded-version-of-core/
- http://www.concrete5.org/community/forums/customizing_c5/setting-up-cron-job-script-include-just-the-and039functions-anda/
...which indicate you also need to declare the DIR_BASE
constant... but this may be an old requirement because I got my tests to run just fine without it.
回答2:
To write an import script, I'd suggest to add a single page to the dashboard, as described here. It's realy simple and will handle the bootstrapping for you.
来源:https://stackoverflow.com/questions/15041252/bootstrapping-c5-from-an-external-script