I found this today while searching the web exactly for how to do that, but the answer wasn't very easy to find.
https://hibern8.wordpress.com/2018/09/25/drupal-8-bootstrap-from-external-script/
Reproducing here in case the site goes offline.
As this code is from 2018, it should work with Drupal 8, maybe Drupal 8.9, and hopefully with Drupal 9 and 10. I will test when I have time.
Bootstrap Drupal 8 from external script
Posted on September 25, 2018 by hibern8
Today i was trying to access my Drupal 8 site from an external php script, i ended up with this code:
<?php namespace Drupal\Core; define('DRUPAL_DIR', '/var/www/drupal8'); define('DRUPAL_SITE', 'default'); $autoloader = require_once DRUPAL_DIR.'/autoload.php'; use Drupal\Core\Site\Settings; use Drupal\Core\DrupalCliKernel; class DrupalCliKernel extends DrupalKernel { public function bootstrap() { static::bootEnvironment(); $this->setSitePath('sites/'.DRUPAL_SITE); Settings::initialize($this->root, 'sites/'.DRUPAL_SITE, $this->classLoader); $this->boot(); } } $kernel = new DrupalCliKernel('prod', $autoloader); $kernel->bootstrap(); // Drupal is bootstrapped now.
- Log in to post comments
Comments