Question

In Drupal 7, I was able to use bootstrap.inc in an external PHP script, which was a custom set of pages that simply bootstrapped Drupal to check the user was logged in Drupal.

The following code is a very basic version that just returns the username of an active session.

require_once '../includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;
echo $user->name;

I'm looking to implement something similar in Drupal 8. The same script returns a server error.

Error: Class 'Drupal\Core\Session\AccountInterface' not found in core/includes/bootstrap.inc, line 63

How should I check for an active user session from an external script, in Drupal 8?

Was it helpful?

Solution

You can't really do something like that in Drupal 8.

The short story is that we use Symphony for the Request / Response flow, so actually bootsrapping Drupal is a lot more complex than simply requiring the bootstrap.inc file. If you look at the index.php file, you can see an overview of a request in Drupal 8:

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();

$kernel->terminate($request, $response);

Most of this happens in the DrupalKernal::handle call. You could probably make a custom script that hooks into Drupal, but it probably will be a lot easier and more maintable to actually have this code be part of your Drupal installation.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top