Skip to content

Instantly share code, notes, and snippets.

@thelebster
Created January 24, 2021 09:45
Show Gist options
  • Select an option

  • Save thelebster/ed8fd3b89bcfe41d8a8a6de9d9ca0afb to your computer and use it in GitHub Desktop.

Select an option

Save thelebster/ed8fd3b89bcfe41d8a8a6de9d9ca0afb to your computer and use it in GitHub Desktop.
Drupal: Execute standalone PHP script

Drupal: Execute standalone PHP script

Install Drush - a command-line shell and Unix scripting interface for Drupal.

Basic example

Place file script.php under the Drupal root directory:

<?php

use Drupal\Core\Update\UpdateKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once 'core/includes/bootstrap.inc';

$database = \Drupal::database();
// Do something.

To execute a standalone php script, run drush scr script.php.

Execute from a subfolder

For example if Drupal root directory is web/, place file web/scripts/script.php:

<?php

use Drupal\Core\Update\UpdateKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once dirname(__DIR__) . '/autoload.php';

// Disable garbage collection during test runs. Under certain circumstances the
// update path will create so many objects that garbage collection causes
// segmentation faults.
require_once dirname(__DIR__) . '/core/includes/bootstrap.inc';

$database = \Drupal::database();
// Do something.

To execute a standalone php script from the subfolder, run drush scr web/scripts/script.php.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment