How do I use the \Drupal class in an standalone PHP file?

3 Answers 3


You can make use of drush php-script command.

drush scr /path/to/file.php

You can use drush in a shebang, if you have drush installed.

This is an example for a standalone php file, where you can access Drupal classes:

#!/usr/bin/env drush

$sitename = \Drupal::config('system.site')->get('name');

echo "This is the site $sitename\n";

See http://www.drush.org/en/master/shellscripts/


Since most of the drupal core components are composer packages you can load them quite easily using composer:

composer require drupal/component_name

You just need to require the composer's autoloader into your PHP script and you can quite easily use these classes in your own scripts.

Your Answer

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

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