2

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?

1 Answer 1

3

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.

2
  • Interesting. I have a node.js application running on port 8080, that basically calls this script to make sure user has logged into drupal before running the app. I know just enough about drupal and PHP to get me into trouble... I've noticed there are webservices modules as part of the core now in drupal 8, but no "idiots" guide to setting something up. I spent a good deal of time with the documentation but I'm no closer to knowing how to proceed. Any good advice or links?
    – tremor
    Mar 29, 2016 at 13:28
  • @Frodo A very simple solution is to call /user, Drupal will redirect to user/uid if the user is logged in. Otherwise you can making a custom route / Controller docs: drupal.org/developing/api/8/routing
    – googletorp
    Mar 29, 2016 at 13:37

Your Answer

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

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