Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Drush

Drush is a program for managing a Drupal site from the terminal. Everything it does can also be done in the panel, but a command is faster: rebuild the cache, enable a module, log in as the administrator. A developer runs it dozens of times a day.

Running it

Drush was installed into the project in the previous lesson and lives in vendor/bin. It is run from the project folder:

vendor/bin/drush status

The command prints the Drupal version, the PHP version, the database name and the site path. If you see an error instead, you are not in the project folder.

From here on the course writes just drush for short — it means vendor/bin/drush.

Rebuilding the cache

vendor/bin/drush cache:rebuild

The short form is drush cr. Drupal remembers the list of routes, hooks, services, templates and plugins so that it does not have to look for them on every request. That is why, after you change module files, nothing new shows up until the cache is rebuilt. You need this command after every lesson of the course.

Main commands

CommandShortWhat it does
drush cache:rebuilddrush crRebuilds the cache
drush pm:install blogdrush en blogEnables a module
drush pm:uninstall blogdrush pmu blogUninstalls a module and removes its data
drush pm:listdrush pmlThe list of modules and their state
drush user:logindrush uliA one-time link to log in as the administrator
drush watchdog:showdrush wsThe latest log entries
drush cronRuns the scheduled tasks
drush statusdrush stInformation about the site

drush help shows help for any command, for example drush help pm:install.

Installing a ready-made module

Ready-made modules from drupal.org are downloaded by Composer and enabled by Drush. Let us install Admin Toolbar — it adds drop-down menus to the panel:

composer require drupal/admin_toolbar
vendor/bin/drush en admin_toolbar

The first command puts the module into web/modules/contrib/admin_toolbar, the second enables it. After a page reload the items of the top panel menu open on hover.

Logging in without a password

vendor/bin/drush uli

The command prints an address like http://default/user/reset/1/.... Open it in the browser, replacing default with 127.0.0.1:8888, and you are logged in as the administrator.

Common mistake

You added a route, a hook or a template, and nothing changed on the site. The cause is almost always the cache: run drush cr. Make it a rule to run this command after any change to .yml files, after adding a hook and after adding a new plugin class.

Thanks for reading!

Donate to the site — we work hard for you.